zoukankan      html  css  js  c++  java
  • iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义

    1、UIGestureRecognizer 介绍

    手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性。

    iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作。

    • UIPanGestureRecognizer(拖动)

    • UIPinchGestureRecognizer(捏合)

    • UIRotationGestureRecognizer(旋转)

    • UITapGestureRecognizer(点按)

    • UILongPressGestureRecognizer(长按)

    • ​UISwipeGestureRecognizer(轻扫)

    另外,可以通过继承 UIGestureRecognizer 类,实现自定义手势(手势识别器类)。

    自定义手势时,需要 #import <UIKit/UIGestureRecognizerSubclass.h>,一般需实现如下方法:

    - (void)reset;
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
    //以上方法在分类 UIGestureRecognizer (UIGestureRecognizerProtected) 中声明,更多方法声明请自行查看

    手势状态枚举如下:

    1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    2     UIGestureRecognizerStatePossible,   // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
    3     UIGestureRecognizerStateBegan,      // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
    4     UIGestureRecognizerStateChanged,    // 手势状态发生转变
    5     UIGestureRecognizerStateEnded,      // 手势识别操作完成(此时已经松开手指)
    6     UIGestureRecognizerStateCancelled,  // 手势被取消,恢复到默认状态
    7     UIGestureRecognizerStateFailed,     // 手势识别失败,恢复到默认状态
    8     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded
    9 };

    使用手势很简单,分为三步:

    1. 创建手势识别器对象实例。创建时,指定一个回调方法,当手势开始,改变、或结束时,执行回调方法。

    2. 设置手势识别器对象实例的相关属性(可选部分)

    3. 添加到需要识别的 View 中。每个手势只对应一个 View,当屏幕触摸在 View 的边界内时,如果手势和预定的一样,那就会执行回调方法。

    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [imgVCustom addGestureRecognizer:recognizer];
  • 相关阅读:
    PHP页面跳转的几种方法
    PHP网站并发测试
    04-上传文件
    01-转>linux命令
    01-CDN的好处
    05-socket.io使用
    04-soket.io使用2 -数据同步简单聊天室效果
    03-socket.io 2.3.0版本的使用-用户请求接口,实时推送给前端数据
    02-转>
    跨域-转>预解析OPTIONS请求
  • 原文地址:https://www.cnblogs.com/fengmin/p/6144220.html
Copyright © 2011-2022 走看看