• 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;

        }

     }

  • 相关阅读:
    20155305乔磊2016-2017-2《Java程序设计》第三周学习总结
    20155305乔磊2016-2017-2《Java程序设计》第二周学习总结
    20155305乔磊2016-2017-2《Java程序设计》第一周学习总结
    20155305乔磊第三次随笔
    乔磊20155305第二次随笔
    乔磊的第一次随笔
    20155302 2016-2017-2 《Java程序设计》第九周学习总结
    20155302 2016-2017-2 《Java程序设计》第八周学习总结
    20155302 2016-2017-2 《Java程序设计》第七周学习总结
    20155302 2016-2017-2 《Java程序设计》第六周学习总结
  • 原文地址:https://www.cnblogs.com/GJ-ios/p/5398241.html
Copyright © 2020-2023  润新知