1. 程式人生 > >iOS圖片新增平移/縮放/旋轉多個手勢

iOS圖片新增平移/縮放/旋轉多個手勢

//
//  UIImageView+Utils.h
//  OpenWorkr
//
//  Created by 冰涼的枷鎖 on 2017/3/6.
//  Copyright © 2017年 Eden. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIImageView (Utils) <UIGestureRecognizerDelegate>

- (void)showBigImageInWindow;

@end
//
//  UIImageView+Utils.m
//  OpenWorkr
//
//  Created by 冰涼的枷鎖 on 2017/3/6.
// Copyright © 2017年 Eden. All rights reserved. // #import "UIImageView+Utils.h" @implementation UIImageView (Utils) static CGRect oldframe; - (void)showBigImageInWindow { UIImageView *currentImageview = self; UIImage *image = currentImageview.image; UIWindow *window = [UIApplication sharedApplication].keyWindow
; UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; backgroundView.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1]; //當前imageview的原始尺寸->將畫素currentImageview.bounds由currentImageview.bounds所在檢視轉換到目標檢視window中,返回在目標檢視window中的畫素值
oldframe = [currentImageview convertRect:currentImageview.bounds toView:window]; // [backgroundView setBackgroundColor:[UIColor colorWithRed:107 green:107 blue:99 alpha:0.6]]; //此時檢視不會顯示 [backgroundView setAlpha:0]; //將所展示的imageView重新繪製在Window中 UIImageView *imageView = [[UIImageView alloc] initWithFrame:oldframe]; [imageView setUserInteractionEnabled:YES]; [imageView setImage:image]; [imageView setTag:10]; [backgroundView addSubview:imageView]; [window addSubview:backgroundView]; //向imageView新增手勢 縮放圖片 UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; //新增到指定檢視 [imageView addGestureRecognizer:pan]; UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)]; [imageView addGestureRecognizer:pinch]; UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)]; [imageView addGestureRecognizer:rotation]; pinch.delegate = self; rotation.delegate = self; //新增點選事件同樣是類方法 -> 作用是再次點選回到初始大小 UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)]; [backgroundView addGestureRecognizer:tapGestureRecognizer]; //動畫放大所展示的ImageView [UIView animateWithDuration:0.4 animations:^{ CGFloat y,width,height; y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5; width = [UIScreen mainScreen].bounds.size.width; //高度 根據圖片寬高比設定 height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width; [imageView setFrame:CGRectMake(0, y, width, height)]; //重要! 將檢視顯示出來 [backgroundView setAlpha:1]; } completion:^(BOOL finished) { }]; } /** * 恢復imageView原始尺寸 * * @param tap 點選事件 */ - (void)hideImageView:(UITapGestureRecognizer *)tap{ UIView *backgroundView = tap.view; //原始imageview UIImageView *imageView = [tap.view viewWithTag:10]; //恢復 [UIView animateWithDuration:0.4 animations:^{ [imageView setFrame:oldframe]; [backgroundView setAlpha:0]; } completion:^(BOOL finished) { //完成後操作->將背景檢視刪掉 [backgroundView removeFromSuperview]; }]; } //建立平移事件 -(void)panAction:(UIPanGestureRecognizer *)pan { UIImageView *imageView = (UIImageView *)pan.view; if (!imageView) { return ; } //獲取手勢的位置 CGPoint position =[pan translationInView:imageView]; //通過stransform 進行平移交換 imageView.transform = CGAffineTransformTranslate(imageView.transform, position.x, position.y); //將增量置為零 [pan setTranslation:CGPointZero inView:imageView]; } //新增捏合事件 -(void)pinchAction:(UIPinchGestureRecognizer *)pinch { UIImageView *imageView = (UIImageView *)pinch.view; if (!imageView) { return ; } //通過 transform(改變) 進行檢視的檢視的捏合 imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale); //設定比例 為 1 pinch.scale = 1; } //旋轉事件 -(void)rotationAction:(UIRotationGestureRecognizer *)rote { UIImageView *imageView = (UIImageView *)rote.view; if (!imageView) { return ; } //通過transform 進行旋轉變換 imageView.transform = CGAffineTransformRotate(imageView.transform, rote.rotation); //將旋轉角度 置為 0 rote.rotation = 0; } #pragma gesture delegate - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } @end