IOS手势识别,捏合,旋转,轻扫等

ref:http://blog.csdn.net/rechard_chen/article/details/51769972
 
//点按手势的创建,这里需要实现响应事件的方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
 
tap.delegate = self;
//  添加到需要手势的视图
[_imageView addGestureRecognizer:tap];
 
// 长按 手势的创建
// 长按手势时间比较长,可以根据手势的状态states,设置功能;
UILongPressGestureRecognizer*longPres = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
 
[_imageView addGestureRecognizer:longPres];
 
// 添加 轻扫手势, 轻扫默认的方向:向右
// 一个轻扫手势只能支持一个方向
// 一个控件可以添加很多手势
UISwipeGestureRecognizer*swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];     //在响应方法中,可以根据轻扫的方向(direction)做事情;
//这是一个枚举类型,上下左右;
swipeLeft.direction= UISwipeGestureRecognizerDirectionLeft;
[_imageView addGestureRecognizer:swipeLeft];
   
UISwipeGestureRecognizer*swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
   
swipeRight.direction= UISwipeGestureRecognizerDirectionRight;
[_imageView addGestureRecognizer:swipeRight];
 
//拖拽手势的创建
UIPanGestureRecognizer *pan = [[ UIPanGestureRecognizer  alloc ] initWithTarget : self  action : @selector (pan:)];
 
[_imageView addGestureRecognizer:pan];
 
- ( void )pan:( UIPanGestureRecognizer *)pan
{
    // 获取手指偏移量,相对于最原始位置的偏移量
   CGPoint transP = [pantranslationInView:_imageView];
    // 改 imageView 形变
   _imageView.transform= CGAffineTransformTranslate(_imageView.transform, transP.x, transP.y);
    // 复位,相对于上一次
    [pansetTranslation:CGPointZeroinView:_imageView];
}
 
//在使用模拟器模拟一下两个手势的时候,需要按住option + 鼠标左键实现两个手指点击(反向运动);shift + option + 鼠标左键(同向运动);
 
//旋转手势的创建
UIRotationGestureRecognizer *rotation = [[ UIRotationGestureRecognizer  alloc ] initWithTarget : self  action :@selector (rotation:)];
rotation.delegate= self;
[_imageView addGestureRecognizer:rotation];
 
//方法的实现
- ( void )rotation:( UIRotationGestureRecognizer *)rotationGestureRecognizer
{
    _imageView . transform = CGAffineTransformRotate ( _imageView . transform , rotationGestureRecognizer. rotation);
    // 复位
    rotationGestureRecognizer.rotation = 0;
}
 
//捏合缩放手势的实现
UIPinchGestureRecognizer *pinch = [[ UIPinchGestureRecognizer  alloc ] initWithTarget : self  action : @selector(pinch:)];
 
pinch.delegate= self;
[_imageView addGestureRecognizer:pinch];
 
//响应方法的实现
- ( void )pinch:( UIPinchGestureRecognizer *)pinch
{
    // 获取相对于最原始的缩放比例
   CGFloat scale = pinch.scale;
    // x,y , x 表示宽度缩放多少, y 表示高度缩放
    _imageView . transform = CGAffineTransformScale ( _imageView . transform , scale, scale);
    // 复位
    pinch.scale = 1;  
}
 
 

手势代理方法的实现:
//当View需要同时实现多个手势的时候。需要返回YES;
- ( BOOL )gestureRecognizer:( UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:( UIGestureRecognizer *)otherGestureRecognizer;
 
//可以控制点击区域是否实现手势;通过UITouch获取当前点击的点,通过坐标控制区域
- ( BOOL )gestureRecognizer:( UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:( UITouch *)touch;
原文地址:https://www.cnblogs.com/lucky-star-star/p/5807130.html