EventHandling for iOS(上)


EventHandling for iOS 总结

一,想法

最近在研究事件处理机制,顺便就把iOS的这块文档看了一下。文档里说了很多,这里准备总结总结,写个简短一点的,比较好看。。。。

二,About Events in iOS

Alt text

iOS这里主要包括三种主要的事件形式、Multi-Touch events, motion events, and events for controlling multimedia 第一种主要是用户触摸屏幕形成的事件、第二种主要是用户移动设备形成的事件、第三种主要是一些远程的控制,比如耳机线控这种。

Gesture Recognizers

Alt text

UIKit会封装一些常用的手势,相比于自己写一些gestures代码,这里可以直接使用UIKit已经写好的,毕竟比较方便。 没一个gesture recognizers都与一个view相关联,当然一个view可以有多个gesture recognizers。

Discrete and Continuous Gestures

对于手势识别,分为两个部分。一个是分离的,一个是连续的。这里分离指的是只出现一次的,比如单击,这种会发一个单一的消息。 一个连续的手势识别会发送一系列的事件消息。 我们要做的是把一个gesture recognizers添加到一个view上,代码如下


- (void)viewDidLoad {
       [super viewDidLoad];
       // Create and initialize a tap gesture
       UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
            initWithTarget:self action:@selector(respondToTapGesture:)];
       // Specify that the gesture must be a single tap
       tapRecognizer.numberOfTapsRequired = 1;
       // Add the tap gesture recognizer to the view
       [self.view addGestureRecognizer:tapRecognizer];
       // Do any additional setup after loading the view, typically from a nib
  }

Responding to Discrete Gestures

Alt text

左边图片是分离的事件,右边是连续的事件。 每次当一个状态改变的时候,gesture recognizer 发送一些消息给目标。一个间断的手势 识别仅仅发送一条简单的消息,而连续的事件会发送多条消息。

Interacting with Other Gesture Recognizers

你可以添加很多gesture recognizer,这里就遇到一个顺序的问题,事实上这里并没有一个顺序来表明是哪个gesture recognizers先收到 touch事件。

这里有一个问题,比如单击与双击事件同时都监听咯。用户只要单击,这样就会触发单击事件,就一直不会触发这个双击事件。这里我们如何去处理。 这里使用到了 requireGestureRecognizerToFail,来表明。self.panRecognizer 必须等到self.swipeRecognizer失败以后才会去分析 这个事件。


- (void)viewDidLoad {
       [super viewDidLoad];
}
// Do any additional setup after loading the view, typically from a nib
[self.panRecognizer requireGestureRecognizerToFail:self.swipeRecognizer];

Preventing Gesture Recognizers from Analyzing Touches

有时候我们会想着,阻止一些事件的响应,这里有两种主要的方式。 gestureRecognizer:shouldReceiveTouch: 这个方法主要是在当一个事件还没出现之前就知道当前的view是否 接收这个事件,比如有一些页面就是不想让他单击跳转等。


- (void)viewDidLoad {
    [super viewDidLoad];
    // Add the delegate to the tap gesture recognizer
    self.tapGestureRecognizer.delegate = self;
}
// Implement the UIGestureRecognizerDelegate method
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
    // Determine if the touch is inside the custom subview
    if ([touch view] == self.customSubview){
        // If it is, prevent all of the delegate's gesture recognizers
        // from receiving the touch
        return NO;
}
return YES; 
}

当然,有些地方可能你一开始并不是那么确定。必须有了一定的分析过后才能决定是否阻止这个gesture recognizer 此方法在gesture recognizer视图转出UIGestureRecognizerStatePossible状态时调用,如果返回NO,则转换到 UIGestureRecognizerStateFailed;如果返回YES,则继续识别触摸序列.(默认情况下为YES)

Permitting Simultaneous Gesture Recognition

假如你这边想着能够同时识别两种手势,这时候就需要用到gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 这里只要两个手势,有一个返回yes,就表明可以同时识别

Specifying a One-Way Relationship Between Two Gesture Recognizers

现在你又需要干另外一件事情,你想要一种手势识别阻止另一种手势识别,你需要用到 canPreventGestureRecognizer

Interacting with Other User Interface Controls

对于一些特定的UIControl的控制事件,如果在当前的view上添加gesture recognizer.这里并不会收到事件,如果你想收到的话,就需要 把gesture recognizer 直接添加到这个控制控制上去。

A single finger single tap on a UIButton, UISwitch, UIStepper, UISegmentedControl, and UIPageControl. A single finger swipe on the knob of a UISlider, in a direction parallel to the slider. A single finger pan gesture on the knob of a UISwitch, in a direction parallel to the switch.

UIEvent UITouch

A touch 表明是一个手指在屏幕上的移动。一个gesture包含一个或者多个touch. An event 包含了在一次多点触控过程中所有的touches。 这个多点触控的过程开始于一根手指触摸到屏幕,结束于最后一根手指 离开屏幕。

An App Receives Touches in the Touch-Handling Methods

在多点触控的过程中,app会有一些回调。 touchesBegan:withEvent: method when one or more fingers touch down on the screen. touchesMoved:withEvent: method when one or more fingers move. touchesEnded:withEvent: method when one or more fingers lift up from the screen. touchesCancelled:withEvent: method when the touch sequence is canceled by a system event, such as an incoming phone call.

这个地方需要分清楚这些方法指的是touch的事件,和gesture是不同的。这些和gesture recognizer的状态并没有必然的相关性。下面这段话,可以理解一下。 These methods are not associated with gesture recognizer states, such as UIGestureRecognizerStateBegan and UIGestureRecognizerStateEnded. Gesture recognizer states strictly denote the phase of the gesture recognizer itself, not the phase of the touch objects that are being recognized.

Back to blog