iOS:事件处理机制(一)--Gesture Recognizers(待续)

来源:互联网 发布:淘宝神笔怎么保存模板 编辑:程序博客网 时间:2024/04/28 22:23

         官方文档说明:《Event Handling Guide for iOS》,本文参考转载文章,并参照官方文档补充说明。

         本篇内容将围绕iOS中事件及其传递机制进行学习和分析。在iOS中,事件分为三类:


  • 触控事件(单点、多点触控以及各种手势操作)
  • 传感器事件(重力、加速度传感器等)
  • 远程控制事件(远程遥控iOS设备多媒体播放等)

         这三类事件共同构成了iOS设备丰富的操作方式和使用体验,本次就首先来针对第一类事件:触控事件,进行学习和分析。

      Gesture Recognizers

        Gesture Recognizers是一类手势识别器对象,它可以附属在你指定的View上,并且为其设定指定的手势操作,例如是点击、滑动或者是拖拽。当触控事件 发生时,设置了Gesture Recognizers的View会先通过识别器去拦截触控事件,如果该触控事件是事先为View设定的触控监听事件,那么Gesture Recognizers将会发送动作消息给目标处理对象,目标处理对象则对这次触控事件进行处理,先看看如下流程图。

            在iOS中,View就是我们在屏幕上看到的各种UI控件,当一个触控事件发生时,Gesture Recognizers会先获取到指定的事件,然后发送动作消息(action message)给目标对象(target),目标对象就是ViewController,在ViewController中通过事件方法完成对该事件的处理。Gesture Recognizers能设置诸如单击、滑动、拖拽等事件,通过Action-Target这种设计模式,好处是能动态为View添加各种事件监听,而不用去实现一个View的子类去完成这些功能。

            以上过程就是我们在开发中在方法中常见的设置action和设置target,例如为UIButton设置监听事件等。

            常用手势识别类

           在UIKit框架中,系统为我们事先定义好了一些常用的手势识别器,包括点击、双指缩放、拖拽、滑动、旋转以及长按。通过这些手势识别器我们可以构造丰富的操作方式。

         在上表中可以看到,UIKit框架中已经提供了诸如UITapGestureRecognizer在内的六种手势识别器,如果你需要实现自定义的手势识别器,也可以通过继承UIGestureRecognizer类并重写其中的方法来完成,这里我们就不详细讨论了。

        Gesture Recognizers Are Attached to a View

         每一个Gesture Recognizer关联一个View,但是一个View可以关联多个Gesture Recognizer,因为一个View可能还能响应多种触控操作方式。为了使gesture recognizer识别发生在view上面的手势,你必须attach gesture recognizer to that view。当一个触控事件发生时,Gesture Recognizer接收一个touch发生的消息要先于View本身,结果就是Gesture Recognizer可以代表view回应视图上的touches事件。

       Gestures Trigger Action Messages 

        当gesture recognizer识别了一个特定手势,它就会发送一条动作消息(action message)给它的target,所以创建gesture recognizer时候,需要initialize it with a targer and action。

       连续和不连续动作

         触控动作同时分为连续动作(continuous)和不连续动作(discrete),连续动作例如滑动和拖拽,它会持续一小段时间,而不连续动作例如单击,它瞬间就会完成,在这两类事件的处理上又稍有不同。对于不连续动作,Gesture Recognizer只会给ViewContoller发送一个单一的动作消息(action message),而对于连续动作,Gesture Recognizer会发送多条动作消息给ViewController,直到所有的事件都结束。

        Responding to Events with Gesture Recognizers(使用手势识别)

        There are three things you do to add a built-in gesture recognizer to your app:

  • Create and configure a gesture recognizer instance. This step includes assigning a target, action, and sometimes assigning gesture-specific attributes (such as the numberOfTapsRequired).
  • Attach the gesture recognizer to a view.
  • Implement the action method that handles the gesture.

         添加GestureRecognizer                  

        为一个View添加GestureRecognizer有两种方式,一种是通过InterfaceBuilder实现

        1.add a gesture recognizer to your app the same way you add any object to your user interface—drag the gesture recognizer from the object library to a view. When you do this, the gesture recognizer automatically becomes attached to that view. You can check which view your gesture recognizer is attached to, and if necessary, change the connection in the nib file.

         2.After you create the gesture recognizer object, you need to create and connect an action method. This method is called whenever the connected gesture recognizer recognizes its gesture

         3.If you need to reference the gesture recognizer outside of this action method, you should also create and connect anoutlet for the gesture recognizer.

@interface APLGestureRecognizerViewController ()@property (nonatomic, strong) IBOutlet UITapGestureRecognizer *tapRecognizer;@end @implementation- (IBAction)displayGestureForTapRecognizer:(UITapGestureRecognizer *)recognizer     // Will implement method later...}@end

       另一种就是通过代码实现,我们看看通过代码来如何实现。

//MyViewContoller.m- (void)viewDidLoad{     [superviewDidLoad];     // 创建并初始化手势对象     UITapGestureRecognizer*tapRecognizer = [[UITapGestureRecognizeralloc]          initWithTarget:selfaction:@selector(respondToTapGesture:)];     // 指定操作为单击一次     tapRecognizer.numberOfTapsRequired= 1;     // 为当前View添加GestureRecognizer     [self.viewaddGestureRecognizer:tapRecognizer];     // ...}

         Responding to Discrete/Continuous  Gestures

         待续……

        Defining How Gesture Recognizers Interact   

        在事件处理过程中,这两种方式所处的状态又各有不同,首先,所有的触控事件最开始都是处于可用状态(Possible),对应UIKit里面的UIGestureRecognizerStatePossible类,如果是不连续动作事件,则状态只会从Possible转变为已识别状态(Recognized,UIGestureRecognizerStateRecognized)或者是失败状态(Failed,UIGestureRecognizerStateFailed)。例如一次成功的单击动作,就对应了Possible-Recognized这个过程。

            如果是连续动作事件,如果事件没有失败并且连续动作的第一个动作被成功识别(Recognized),则从Possible状态转移到Began(UIGestureRecognizerStateBegan)状态,这里表示连续动作的开始,接着会转变为Changed(UIGestureRecognizerStateChanged)状态,在这个状态下会不断循环的处理连续动作,直到动作执行完成变转变为Recognized已识别状态,最终该动作会处于完成状态(UIGestureRecognizerStateEnded),另外,连续动作事件的处理状态会从Changed状态转变为Canceled(UIGestureRecognizerStateCancelled)状态,原因是识别器认为当前的动作已经不匹配当初对事件的设定了。每个动作状态的变化,Gesture Recognizer都会发送消息(action message)给Target,也就是ViewController,它可以根据这些动作消息进行相应的处理。例如一次成功的滑动手势动作就包括按下、移动、抬起的过程,分别对应了Possible-Began-Changed-Recognized这个过程。

     Gesture Recognizers Interpret Raw Touch Events

        在屏幕上的每一次动作事件都是一次Touch,在iOS中用UITouch对象表示每一次的触控,多个Touch组成一次Event,用UIEvent来表示一次事件对象。


          在上述过程中,完成了一次双指缩放的事件动作,每一次手指状态的变化都对应事件动作处理过程中得一个阶段。通过Began-Moved-Ended这几个阶段的动作(Touch)共同构成了一次事件(Event)。在事件响应对象UIResponder中有对应的方法来分别处理这几个阶段的事件。

  • touchesBegan:withEvent:
  • touchesMoved:withEvent:
  • touchesEnded:withEvent:
  • touchesCancelled:withEvent:

        后面的参数分别对应UITouchPhaseBegan、UITouchPhaseMoved、UITouchPhaseEnded、UITouchPhaseCancelled这几个类。用来表示不同阶段的状态。

       Regulating the Delivery of Touches to Views

         待续……

       Creating a Custom Gesture Recognizer

         待续……
------------------------------------
参考:
《Event Handling Guide for iOS》https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html
《iOS事件机制(一)》http://ryantang.me/blog/2013/12/07/ios-event-dispatch-1/

0 0
原创粉丝点击