zoukankan      html  css  js  c++  java
  • Gesture 不同手势使用

    #import "ViewController.h"

    @interface ViewController () {

        UIView *_view;

    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        _view = [[UIView alloc] initWithFrame:CGRectMake(90, 90, 20, 20)];

        _view.backgroundColor = [UIColor redColor];

        [self.view addSubview:_view];

        

        [self _initPinchGesture];

    }

    //--------------------轻击手势---------------------------

    - (void)_initTap {

        //单击手势

        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];

        //设置点击的次数

        tap1.numberOfTapsRequired = 1;

        //设置手指的个数

        tap1.numberOfTouchesRequired = 1;

        //将手势添加到视图上

        [self.view addGestureRecognizer:tap1];

        

        //双击手势

        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleAction:)];

        tap2.numberOfTapsRequired = 2;

        //添加手势

        [self.view addGestureRecognizer:tap2];

        

        //区别两个手势

        [tap1 requireGestureRecognizerToFail:tap2];

    }

    //单击手势响应的事件

    - (void)singleTapAction:(UITapGestureRecognizer *)tap {

        //关闭手势

    //    tap.enabled = NO;

        

        //获取手势所在的视图

        UIView *view = tap.view;

        view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(10)/10.0 green:arc4random_uniform(10)/10.0 blue:arc4random_uniform(10)/10.0 alpha:1];

        

        NSLog(@"singleTapAction");

    }

    //双击手势响应的事件

    - (void)doubleAction:(UITapGestureRecognizer *)tap {

        NSLog(@"doubleAction");

    }

    //--------------------轻扫手势---------------------------

    - (void)_initSwipeGesture {

        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];

        //设置轻扫的方向

        /*

         UISwipeGestureRecognizerDirectionRight

         UISwipeGestureRecognizerDirectionLeft

         UISwipeGestureRecognizerDirectionUp

         UISwipeGestureRecognizerDirectionDown

         */

        swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;

        [self.view addGestureRecognizer:swipeGesture];

    }

    - (void)swipeAction:(UISwipeGestureRecognizer *)swipe {

        NSLog(@"swipeAction");

    }

    //--------------------平移手势---------------------------

    - (void)_initPanGesture {

        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

        [self.view addGestureRecognizer:panGesture];

    }

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

        //手指所在的坐标

        CGPoint point = [pan locationInView:self.view];

        _view.center = point;

    }

    //--------------------长按手势---------------------------

    - (void)_initLongGesture {

        

        UILongPressGestureRecognizer *pressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressAction:)];

        //设置最短时间

        pressGesture.minimumPressDuration = 1;

        [self.view addGestureRecognizer:pressGesture];

        

    }

    - (void)pressAction:(UILongPressGestureRecognizer *)press {

        /*

         UIGestureRecognizerStateBegan   开始

         UIGestureRecognizerStateChanged    改变

         UIGestureRecognizerStateEnded   结束

         UIGestureRecognizerStateCancelled  取消

         */

        

        if (press.state == UIGestureRecognizerStateBegan) {

            NSLog(@"pressAction");

        }

    }

    //--------------------旋转手势---------------------------

    - (void)_initRotationGesture {

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

        imgView.userInteractionEnabled = YES;

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

        [self.view addSubview:imgView];

        

        UIRotationGestureRecognizer *rotationGresutre = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];

        [imgView addGestureRecognizer:rotationGresutre];

    }

    - (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture {

        NSLog(@"%f",rotationGesture.rotation);

        UIImageView *imgView = (UIImageView *)rotationGesture.view;

        

        if (rotationGesture.state == UIGestureRecognizerStateChanged) {

            

            imgView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);

            

        }else if (rotationGesture.state == UIGestureRecognizerStateEnded || rotationGesture.state == UIGestureRecognizerStateCancelled) {

        

            [UIView animateWithDuration:0.3 animations:^{

                

                imgView.transform = CGAffineTransformIdentity;

            }];

            

        }

        

    }

    //--------------------缩放手势---------------------------

    - (void)_initPinchGesture {

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

        imgView.userInteractionEnabled = YES;

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

        [self.view addSubview:imgView];

        

        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

        [imgView addGestureRecognizer:pinchGesture];

        

    }

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

    //    pinch.scale;    //缩放比

        NSLog(@"pinchAction:%f",pinch.scale);

        

        UIImageView *imgView = (UIImageView *)pinch.view;

        if (pinch.state == UIGestureRecognizerStateChanged) {

            

            imgView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);

            

        }else if (pinch.state == UIGestureRecognizerStateEnded || pinch.state == UIGestureRecognizerStateCancelled) {

            

            [UIView animateWithDuration:0.3 animations:^{

                

                imgView.transform = CGAffineTransformIdentity;

            }];

            

        }

        

    }

    @end

  • 相关阅读:
    jstl标签
    get和post
    try中的局部变量在finally中是找不到的。
    bzoj 4408: [Fjoi 2016]神秘数 数学 可持久化线段树 主席树
    ZOJ2112 BZOJ1901 Dynamic Rankings 树套树 带修改的区间第k小
    BZOJ 2120: 数颜色 带修改的莫队算法 树状数组套主席树
    POJ2104 K-th Number 不带修改的主席树 线段树
    POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
    POJ1151 Atlantis 水题 计算几何
    BZOJ 2333: [SCOI2011]棘手的操作 可并堆 左偏树 set
  • 原文地址:https://www.cnblogs.com/answer-Allen/p/4815330.html
Copyright © 2011-2022 走看看