zoukankan      html  css  js  c++  java
  • iOS手势总结

    手势并发执行的方法

    1. 设置手势的delegate;实现代理方法
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
    

    手势的优先级设定

    当识别为gesture01时不会立刻触发gesture01,而是检测gesture02是否成功,如果gesture02检测失败后再触发gesture01,达到解决冲突的结果。
    [gesture01 requireGestureRecognizerToFail:gesture02];
    

    点按手势 UITapGestureRecognizer

    1. 创建手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    2. 设置手势属性(可省略)
    tap.numberOfTapsRequired = 1; 需要点击的次数,默认是1.
    tap.numberOfTouchesRequired = 1; 需要点击的手指数,默认是1.
    [tap requireGestureRecoginzerToFail:doubleTap]; doubleTap是自定义的一个双击事件.使用这个方法可以取消掉单击和双击事件并存的情况.
    3. 添加手势
    [self.imgView addGestureRecognizer:tap];
    4. 实现方法
    - (void)tap:(UITapGestureRecoginzer *)tapGestureRecognizer{
        // 点击后执行的方法
    }
    

    长按手势 UILongPressGestureRecognizer

    1. 创建手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    2. 设置属性
    longPress.minimumPressDuration = 0.5; 至少0.5秒后触发
    3. 添加手势
    [self.imgView addGestureRecognizer:longPress];
    4. 实现方法 与状态配合实现
    - (void)longPress:(UILongPressGestureRecognizer *)longPressGesture {
        if(longPressGesture.state == UIGestureRecognizerStateBegan) {
            // 长按手势开始
        }
        if(longPressGesture.state == UIGestureRecognizerStateChanged) {
            // 长按手势 手指位置变化
        }
        if(longPressGesture.state == UIGestureRecognizerStateFailed) {
          // 手势识别失败,恢复默认状态
        }
        if(longPressGesture.state == UIGestureRecognizerStateCancelled) {
            // 手势取消,恢复默认状态
        }
        if(longPressGesture.state == UIGestureRecognizerStateRecognized) {
            // 手势状态已响应
        }
        if(longPressGesture.state == UIGestureRecognizerStateEnded) {
            // 长按手势 状态结束
        }
    }
    

    轻扫手势,滑动 UISwipeGestureRecognizer

    创建手势,滑动手势有四个方向,如果处理方法相同则可以使用“按位或 | ”设置滑动方向,若处理方法不同则需要创建多个对象添加
    UISwipeGestureRecognizer *swipe01 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipe01.direction = UISwipeGestureRecognizerDirectionDown;
    设置手势的优先级用来解决冲突
    [swipe requireGestureRecoginzerToFail:pan];
    [self.imgView addGestureRecognizer: swipe01];
    
    UISwipeGestureRecognizer *swipe02 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipe02.direction = UISwipeGestureRecognizerDirectionUp;
    [self.imgView addGestureRecognizer: swipe02];
    
    UISwipeGestureRecognizer *swipe03 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipe03.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
    [self.imgView addGestureRecognizer: swipe03];
    
    实现方法
    typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
        UISwipeGestureRecognizerDirectionRight = 1 << 0,
        UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
        UISwipeGestureRecognizerDirectionUp    = 1 << 2,
        UISwipeGestureRecognizerDirectionDown  = 1 << 3
    };
    - (void)swipe:(UISwipeGestureRecognizer *)swipeGesture {
        // 判断方向实现不同方法 向左向右不能执行因为状态为:3;
        if(swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
            // 向左滑动
        }
        if(swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {
            // 向右滑动
        }
        if(swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {
            // 向上滑动
        }
        if(swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {
            // 向下滑动
        }
    }
    

    拖动手势 UIPanGestureRecognizer

    1. 创建手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    2. 添加手势
    [self.imgView addGestureRecognizer:pan];
    3. 实现方法
    - (void)pan:(UIPanGestureRecognizer *)pan {
        1. localInView 是触摸点在View的位置
        2. translationInView 是点位移的位置
        3. 手势.view 是手势添加在View身上
        CGPoint translation = [pan translationInView:pan.view];
        CGPoint center = pan.view.center;
        center.x = center.x + translation.x;
        center.y = center.y + translation.y;
        pan.view.center = center;
        
        // 复位
        [pan setTranslation:CGPointZero inView:pan.view];
    }
    

    旋转手势 UIRotationGestureRecognizer

    1. 创建手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    2. 添加手势
    [self.imgView addGestureRecognizer:rotation];
    3. 实现方法
    - (void)rotation:(UIRotationGestureRecognizer *)rotationGesture {
        //获取旋转的角度
        CGFloat scale = rotationGesture.rotation;
        // 设置view的角度,使用transform设置
        rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, scale);
        
        // 复位
        rotationGesture.rotation = 0;
    }
    

    捏合手势 缩放 UIPinchGestureRecognizer

    1. 创建手势
    UIPinchGestureRecognizer *pinch = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    2. 添加手势
    [self.imgView addGestureRecognizer:pinch];
    3. 实现方法
    - (void)pinch:(UIPinchGestureRecognizer *)pinchGesture {
        //获取缩放值
        CGFloat scale = pinchGesture.scale;
        // 设置值
        pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, scale, scale);
        // 复位
        pinchGesture.scale = 1;
    }
    

    屏幕边缘滑动手势 UIScreenEdgePanGestureRecognizer

    screenEdgePan是UIPanGestureRecognizer的子类,用法相同,不过screenEdgePan是专用于响应从屏幕边缘滑动的效果。
    可设置四个方向的效果.
    screenEdgePan.edges = UIRectEdgeAll;
    screenEdgePan.edges = UIRectEdgeLeft | UIRectEdgeRight;


    作者:Elenx
    链接:https://www.jianshu.com/p/bfe87f69c804
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    MIPS——分支语句
    MIPS简单入门
    迷宫问题——最短路
    用dfs遍历联通块(优化)
    用protractor測试canvas绘制(二)
    android 用java代码设置布局、视图View的宽度/高度或自适应
    HBase编程实例
    Top10Servlet
    Delete Node in a Linked List
    atitit.html5动画特效----打水漂 ducks_and_drakes
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/13187448.html
Copyright © 2011-2022 走看看