Fork me on GitHub

UI- 五种手势识别总结

#pragma mark - 手势  总共有五种手势  分别为 Tap点击 Pan拖拽 LongPress长时间按压 Pinch捏合手势 rotation旋转

 1. 定义成员变量

    UIImageView *_imgView; 定义UIImageView, 响应手势方法时调用

    CGPoint originalCenter;  记录一下拖拽手势起始位置

    CGAffineTransform  originalTrans; 记录一下捏合、旋转手势起始位置

 2. 定义ImageView

    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];

    imgView.image = [UIImage imageNamed:@"btn_02"];

    [self.view addSubview:imgView];

    _imgView = imgView; //赋值

 3. 打开与用户的交互能力 (至关重要,不能忘记)

    imgView.userInteractionEnabled = YES;

 4.Tap手势 --> 点击1次/两次

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onceTapped )];

    把这个手势添加到视图中

    [imgView addGestureRecognizer:tap];

 

    UITapGestureRecognizer *dblTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twiceTapped)];

    dblTap.numberOfTapsRequired = 2;

    [imgView addGestureRecognizer:dblTap];

    4.1 点击手势响应的方法

    -(void)onceTapped{

    NSLog(@"onceTapped");

    }

 

    -(void)twiceTapped{

    NSLog(@"twiceTapped");

    }

 5.Pan手势 --> 拖拽

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(viewPanned:)];

    [imgView addGestureRecognizer:pan];

    5.1 拖拽时响应的方法

    -(void)viewPanned:(UIPanGestureRecognizer *)pan{

    先记录一下起始位置

    if (pan.state == UIGestureRecognizerStateBegan) {

    originalCenter = imgView.center;

    }

 

    CGPoint transPoint = [pan translationInView:_imgView];

 

    CGPoint center = originalCenter;

    center.x += transPoint.x;

    center.y += transPoint.y;

 

    imgView.center = center;

 

    CGPoint velocityPoint = [pan velocityInView:_imgView];

    }

6. LongPress手势 --> 长时间按压

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self  action:@selector(longPressed )];

    [imgView addGestureRecognizer:longPress];

    longPress.minimumPressDuration = 1;

    6.1 长时间按压响应的方法

    -(void)longPressed{

    NSLog(@"long Press");

    }

7.  Pinch手势 --> 捏合手势

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchImg:)];

    [imgView addGestureRecognizer:pinch];

    7.1 捏合时响应的方法

    -(void)pinchImg:(UIPinchGestureRecognizer *)pinch {

    if (pinch.state == UIGestureRecognizerStateBegan) {

    originalTrans = _imgView.transform;

    }

    _imgView.transform = CGAffineTransformScale(originalTrans, pinch.scale, pinch.scale);

    }

8. rotation手势 --> 旋转

    UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotateImg:)];

    [imgView addGestureRecognizer:rotation];

 8.1 旋转手势响应的方法

    -(void)rotateImg:(UIRotationGestureRecognizer *)rotation{

    if (rotation.state == UIGestureRecognizerStateBegan) {

    originalTrans = _imgView.transform;

    }

 

    _imgView.transform = CGAffineTransformRotate(originalTrans, rotation.rotation);

 

    if (rotation.state == UIGestureRecognizerStateEnded) {

    _imgView.transform = originalTrans;

    }

 }

posted @ 2016-04-16 13:39  极度恐慌_JG  阅读(448)  评论(0编辑  收藏  举报