zoukankan      html  css  js  c++  java
  • IOS UIPanGestureRecognizer手势使用及识别状态UIGestureRecognizerState

    UIGestureRecognizerState -- 手势识别器状态

    1.先来看官方文档

    定义UIGestureRecognizer.h

    英文:

    typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
        UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
        
        UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
        UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
        UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
        UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
        
        UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
        
        // Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
        UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    };
    

     中文翻译:

    typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {//手势识别器状态(由UIGestureRecognizer识别器接收识别), 枚举类型
        UIGestureRecognizerStatePossible,   // 识别器还没有识别出它的手势(状态)(Possible),但是可能计算触摸事件。这是默认状态
        
        UIGestureRecognizerStateBegan,      // 识别器已经接收识别为此手势(状态)的触摸(Began)。在下一轮run循环中,响应方法将会被调用。
        UIGestureRecognizerStateChanged,    // 识别器已经接收到触摸,并且识别为手势改变(Changed)。在下一轮run循环中,响应方法将会被调用。
        UIGestureRecognizerStateEnded,      // 识别器已经接收到触摸,并且识别为手势结束(Ended)。在下一轮run循环中,响应方法将会被调用并且识别器将会被重置到UIGestureRecognizerStatePossible状态。
        UIGestureRecognizerStateCancelled,  // 识别器已经接收到触摸,这种触摸导致手势取消(Cancelled)。在下一轮run循环中,响应方法将会被调用。识别器将会被重置到UIGestureRecognizerStatePossible状态。
        
        UIGestureRecognizerStateFailed,     // 识别器已经接收到一个触摸序列,不能识别为手势(Failed)。响应方法将不会被调用,并且识别器将会重置到UIGestureRecognizerStatePossible。
        
        // 离散手势 - 手势识别器识别一个离散事件,但是不会报告改变(例如,一个轻击)不会过度到Began和Changed状态,并且不会失败(fail)或者被取消(cancell)
        UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 识别器接收触摸,并且识别为此手势。在下一轮run循环中,响应方法将会被调用,并且识别将会重置至UIGestureRecognizerStatePossible。
    };
    

    2.名词释义

    手势识别中,run循环是什么[1]?

    APP启动后,IOS会自动创建一个主线程 RunLoop,当发生触摸/锁屏/摇晃等硬件事件(Event)时,系统封装这些事件,并发送给 UIWindow等,交由应用程序进行处理,并由系统回调。这个主线程RunLoop就是run循环。

    3.手势

    系统自带的预定义手势识别器包括(见官方文档)

    UIGestureRecognizer

    • UITapGestureRecognizer(Tap 轻击手势识别器,轻轻点击)
    • UIPinchGestureRecognizer(Pinch 缩放手势识别器)
    • UIRotationGestureRecognizer(Rotation 旋转手势识别器)
    • UISwipeGestureRecognizer(Swipe 轻扫手势识别器,快速滑动)
    • UIPanGestureRecognizer(Pan 平移手势识别器)
    • UIScreenEdgePanGestureRecognizer(ScreenEdgePan 屏幕边缘平移手势识别器)
    • UILongPressGestureRecognizer(LongPress 长按手势识别器)

    4.使用方法

    以Pan为例

    step1:新建IOS工程,工程名Gesture(任意取名)

    step2:在Main.storyboard中添加一个View

    如图1所示

    step3: 在ViewController.m中添加属性, 将View连接至ViewController的mContentView属性

    1 @interface ViewController ()<UIGestureRecognizerDelegate>
    2 @property (nonatomic , strong) UIPanGestureRecognizer *panGestureRecognizer;//Pan手势识别器
    3 @property (nonatomic, assign) CGPoint panStartPoint;//记录触摸起始点
    4 @property (weak, nonatomic) IBOutlet UIView *mContentView;//View连接口
    5 @end

    step4: 初始化 Pan手势识别器

    1 - (void)viewDidLoad {
    2     [super viewDidLoad];
    3     self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognize:)];//初始化平移手势识别器(Pan)
    4     self.panGestureRecognizer.delegate = self;
    5     [self.mContentView addGestureRecognizer:self.panGestureRecognizer];
    6 }

    step5:手势状态处理

     1 -(void)panGestureRecognize:(UIPanGestureRecognizer *)recognize{
     2     switch (recognize.state) {
     3         case UIGestureRecognizerStateBegan:
     4             self.panStartPoint = [recognize translationInView:self.mContentView];
     5             NSLog(@"-----Current State: Began-----");
     6             NSLog(@"start point (%f, %f) in View", self.panStartPoint.x, self.panStartPoint.y);
     7             break;
     8             
     9             case UIGestureRecognizerStateChanged:
    10             NSLog(@"-----Current State: Changed-----");
    11             CGPoint currentPoint = [recognize translationInView:self.mContentView];
    12             NSLog(@"current point (%f, %f) in View", currentPoint.x, currentPoint.y);
    13             break;
    14             
    15         case UIGestureRecognizerStateEnded:
    16             NSLog(@"-----Current State: Ended-----");
    17             CGPoint endPoint = [recognize translationInView:self.mContentView];
    18             NSLog(@"end point (%f, %f) in View", endPoint.x, endPoint.y);
    19             break;
    20             
    21         case UIGestureRecognizerStateCancelled:
    22             NSLog(@"-----Current State: Cancelled-----");
    23             NSLog(@"Touch was cancelled");
    24             break;
    25             
    26         case UIGestureRecognizerStateFailed:
    27             NSLog(@"-----Current State: Failed-----");
    28             NSLog(@"Failed events");
    29        break;
    30         default:
    31             break;
    32     }
    33 }

    5. 测试结果

     1 2015-07-24 00:28:51.998 Gesture[4039:189604] -----Current State: Began-----
     2 2015-07-24 00:28:51.999 Gesture[4039:189604] start point (0.000000, 0.000000) in View
     3 2015-07-24 00:28:52.015 Gesture[4039:189604] -----Current State: Changed-----
     4 2015-07-24 00:28:52.015 Gesture[4039:189604] current point (1.500000, 0.000000) in View
     5 2015-07-24 00:28:52.015 Gesture[4039:189604] -----Current State: Changed-----
     6 2015-07-24 00:28:52.016 Gesture[4039:189604] current point (1.500000, 0.000000) in View
     7 2015-07-24 00:28:52.032 Gesture[4039:189604] -----Current State: Changed-----
     8 2015-07-24 00:28:52.032 Gesture[4039:189604] current point (2.500000, 0.000000) in View
     9 2015-07-24 00:28:52.049 Gesture[4039:189604] -----Current State: Changed-----
    10 2015-07-24 00:28:52.049 Gesture[4039:189604] current point (3.500000, 0.000000) in View
    11 2015-07-24 00:28:52.085 Gesture[4039:189604] -----Current State: Changed-----
    12 2015-07-24 00:28:52.086 Gesture[4039:189604] current point (4.500000, 0.000000) in View
    13 2015-07-24 00:28:52.111 Gesture[4039:189604] -----Current State: Changed-----
    14 2015-07-24 00:28:52.111 Gesture[4039:189604] current point (5.500000, 0.000000) in View
    15 2015-07-24 00:28:52.128 Gesture[4039:189604] -----Current State: Changed-----
    16 2015-07-24 00:28:52.128 Gesture[4039:189604] current point (6.500000, 0.000000) in View
    17 2015-07-24 00:28:52.145 Gesture[4039:189604] -----Current State: Changed-----
    18 2015-07-24 00:28:52.145 Gesture[4039:189604] current point (7.500000, 0.000000) in View
    19 2015-07-24 00:28:52.163 Gesture[4039:189604] -----Current State: Changed-----
    20 2015-07-24 00:28:52.163 Gesture[4039:189604] current point (9.500000, 0.000000) in View
    21 2015-07-24 00:28:52.180 Gesture[4039:189604] -----Current State: Changed-----
    22 2015-07-24 00:28:52.180 Gesture[4039:189604] current point (10.500000, 0.000000) in View
    23 2015-07-24 00:28:52.198 Gesture[4039:189604] -----Current State: Changed-----
    24 2015-07-24 00:28:52.198 Gesture[4039:189604] current point (11.500000, 0.000000) in View
    25 2015-07-24 00:28:52.215 Gesture[4039:189604] -----Current State: Changed-----
    26 2015-07-24 00:28:52.215 Gesture[4039:189604] current point (13.000000, 0.000000) in View
    27 2015-07-24 00:28:52.232 Gesture[4039:189604] -----Current State: Changed-----
    28 2015-07-24 00:28:52.232 Gesture[4039:189604] current point (15.000000, 0.000000) in View
    29 2015-07-24 00:28:52.249 Gesture[4039:189604] -----Current State: Changed-----
    30 2015-07-24 00:28:52.249 Gesture[4039:189604] current point (16.000000, -1.500000) in View
    31 2015-07-24 00:28:52.267 Gesture[4039:189604] -----Current State: Changed-----
    32 2015-07-24 00:28:52.267 Gesture[4039:189604] current point (17.000000, -1.500000) in View
    33 2015-07-24 00:28:52.284 Gesture[4039:189604] -----Current State: Changed-----
    34 2015-07-24 00:28:52.284 Gesture[4039:189604] current point (18.000000, -1.500000) in View
    35 2015-07-24 00:28:52.301 Gesture[4039:189604] -----Current State: Changed-----
    36 2015-07-24 00:28:52.302 Gesture[4039:189604] current point (20.500000, -1.500000) in View
    37 2015-07-24 00:28:52.318 Gesture[4039:189604] -----Current State: Changed-----
    38 2015-07-24 00:28:52.318 Gesture[4039:189604] current point (21.000000, -1.500000) in View
    39 2015-07-24 00:28:52.335 Gesture[4039:189604] -----Current State: Changed-----
    40 2015-07-24 00:28:52.335 Gesture[4039:189604] current point (22.000000, -1.500000) in View
    41 2015-07-24 00:28:52.353 Gesture[4039:189604] -----Current State: Changed-----
    42 2015-07-24 00:28:52.353 Gesture[4039:189604] current point (23.000000, -1.500000) in View
    43 2015-07-24 00:28:52.371 Gesture[4039:189604] -----Current State: Changed-----
    44 2015-07-24 00:28:52.371 Gesture[4039:189604] current point (24.000000, -1.500000) in View
    45 2015-07-24 00:28:52.678 Gesture[4039:189604] -----Current State: Ended-----
    46 2015-07-24 00:28:52.678 Gesture[4039:189604] end point (24.000000, -1.500000) in View

    reference:

    [1] http://blog.sina.com.cn/s/blog_14fdb5d190102vrnl.html
    
  • 相关阅读:
    css实现文字相对于图片垂直居中
    node能做的性能优化
    vue.config.js中vue的配置
    react/vue项目腾讯云/阿里云服务器linux部署
    taro项目结构分析和坑
    vscode自动编译less
    css彩色字体
    微信小程序文件上传
    小程序html解析
    小程序表单
  • 原文地址:https://www.cnblogs.com/fortunely/p/4672052.html
Copyright © 2011-2022 走看看