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

  • 相关阅读:
    sencha touch 扩展篇之将sencha touch打包成安装程序(上)- 使用sencha cmd打包安装程序
    sencha touch 扩展篇之使用sass自定义主题样式 (下)通过css修改官方组件样式以及自定义图标
    一个不错的android组件的网站
    sencha touch 扩展篇之使用sass自定义主题样式 (上)使用官方的api修改主题样式
    sencha touch 入门系列 (九) sencha touch 布局layout
    面试题总结
    国外接活网站Elance, Freelancer和ScriptLance的介绍和对比
    sencha touch 入门系列 扩展篇之sencha touch 项目打包压缩
    Android Design Support Library——Navigation View
    设计模式——命令模式
  • 原文地址:https://www.cnblogs.com/answer-Allen/p/4815330.html
Copyright © 2011-2022 走看看