• 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

  • 相关阅读:
    BZOJ4821 SDOI2017相关分析(线段树)
    BZOJ3167/BZOJ4824 HEOI2013SAO/CQOI2017老C的键盘(树形dp)
    BZOJ4820 SDOI2017硬币游戏(概率期望+高斯消元+kmp)
    BZOJ4811 Ynoi2017由乃的OJ(树链剖分+线段树)
    BZOJ4810 Ynoi2017由乃的玉米田(莫队+bitset)
    Codeforces Round #522 Div. 1 没打记
    BZOJ4784 ZJOI2017仙人掌(树形dp+dfs树)
    43. Multiply Strings
    2. Add Two Numbers
    150. Evaluate Reverse Polish Notation
  • 原文地址:https://www.cnblogs.com/answer-Allen/p/4815330.html
Copyright © 2020-2023  润新知