在设置手势之前一定要将用户交互打开
self.userInteractionEnabled = YES;
若是在做相册的话 设置捏合以及旋转手势之后 想让其在切换之后恢复原样 要抓住切换图片这个时机 切换图片设置的时候一般是轻扫和轻拍 要设置一个属性用来记录初始时候的transform
CGAffineTransform
_transform;
之后将初始的相框属性赋值个给他
_transform
=
self
.
imageView
.
transform
;
在切换图片的时候即在轻拍事件和轻扫事件中
tap.
view
.
transform
=
_transform
;
tap.view代表当前轻拍的那个图片
轻拍手势 1.创建手势对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; 2.配置手势属性 设置轻拍的次数 tap.numberOfTapsRequired = 1; 设置轻拍时手指的个数 tap.numberOfTouchesRequired = 2; 3.将手势添加到指定视图上 [_imageView addGestureRecognizer:tap]; 4.移除手势 [_imageView removeGestureRecognizer:tap]; 设置手势之后一定要记得去实现其事件 轻拍事件 - (void)tapAction:(UITapGestureRecognizer *)tap { 改变图片 if (_isFirst ) { _imageView.image = [UIImage imageNamed:@"2.JPG"]; _isFirst = NO; }else{ _imageView.image = [UIImage imageNamed:@"3.JPG"]; _isFirst = YES; } 平移手势 1.创建对象 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; 添加到指定视图 [_imageView addGestureRecognizer:pan]; 移除手势 [_imageView removeGestureRecognizer:pan]; 平移事件 - (void)panAction:(UIPanGestureRecognizer *)pan { 获取手势在视图上的位置 CGPoint position = [pan translationInView:_imageView]; 进行平移 transform _imageView.transform = CGAffineTransformTranslate(_imageView.transform, position.x, position.y); 将增量置为0 [pan setTranslation:CGPointZero inView:_imageView]; } 轻扫手势 1.创建对象 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)]; 设置轻扫方向 对于一个轻扫手势方向只能设置一对,要么上下,要么左右 如果想都设置,需要再创建一个轻扫手势 swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft; swipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp; 添加到指定视图 [_imageView addGestureRecognizer:swipe]; 移除手势 [_imageView removeGestureRecognizer:swipe]; 轻扫事件 - (void)swipeAction:(UISwipeGestureRecognizer *)swipe { 改变图片 if (_isFirst ) { _imageView.image = [UIImage imageNamed:@"2.JPG"]; _isFirst = NO; }else{ _imageView.image = [UIImage imageNamed:@"3.JPG"]; _isFirst = YES; } } 长按手势 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; //配置属性 //设置最短长按时间 longPress.minimumPressDuration = 1; //设置允许移动的最大距离 longPress.allowableMovement = 10; //添加指定视图 [_imageView addGestureRecognizer:longPress]; 长按事件 - (void)longPressAction:(UILongPressGestureRecognizer *)longPress { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否要保存图片" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil]; 当长按开始的时候弹出提示框 if (longPress.state == UIGestureRecognizerStateBegan) { [alertView show]; } 保存图片 保存到手机相册 UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo) } 旋转手势 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)]; //添加到指定视图 [_imageView addGestureRecognizer:rotation]; //移除手势 [_imageView removeGestureRecognizer:rotation]; 旋转事件 - (void)rotationAction:(UIRotationGestureRecognizer *)rotation { 通过仿射变换 让视图旋转 _imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation); 将旋转角度置为0 rotation.rotation = 0; } 捏合手势 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)]; //添加指定视图 [_imageView addGestureRecognizer:pinch]; //移除手势 [_imageView removeGestureRecognizer:pinch]; //捏合事件 - (void)pinchAction:(UIPinchGestureRecognizer *)pinch { //通过transform 进行捏合 _imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale); pinch.scale = 1; } 屏幕边缘手势 UIScreenEdgePanGestureRecognizer *screenPan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenPanAction:)]; 设置边缘方向 screenPan.edges = UIRectEdgeRight; 添加到指定视图 [_imageView addGestureRecognizer:screenPan]; } 屏幕边缘事件 - (void)screenPanAction:(UIScreenEdgePanGestureRecognizer *)screenPan { NSLog(@"边缘手势触发了"); }