前言
在iOS中,你可以使用系统内置的手势识别(GestureRecognizer),也可以创建自己的手势.GestureRecognizer将低级别的转换为高级别的执行行为,是你绑定到view的对象,当发生手势,绑定到的view对象会响应,它确定这个动作是否对应一个特定的手势(swipe,pinch,pan,rotation).如果它能识别这个手势,那么就会向绑定它的view发送消息,如下图
UIKit框架提供了一些预定义的GestureRecognizer.包含下列手势
- UITapGestureRecognizer敲击手势(单击和双击)
- UIPanGestureRecognizer(拖动手势)
- UIPinchGestureRecognizer(缩放手势)
- UISwipeGestureRecognizer(擦碰手势)
- UIRotationGestureRecognizer(旋转手势)
- UILongPressGestureRecognizer(长按手势)
如果你想让你的应用程序来识别一个独特的手势,如选择目录或纠结的运动,你可以创建自己的自定义GestureRecognizer,将在下篇介绍
将特定的手势和view相关联
每一个特定的手势必须关联到view对象中才会有作用,一个view对象可以关联多个不同的特定手势,但是每一个特定的手势只能与一个view相关联。当用户触摸了view,这个GestureRecognizer就会接受到消息,它可以响应特定的触摸事件。
与特定view关联
- 创建GestureRecognizer实例
- addGestureRecognizer
- 实现处理手势的方法
可以使用removeGestureRecognizer: 来移除手势。
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlerPanGesture:)]; 2 _panGestureRecognizer.delegate = self; 3 _panGestureRecognizer.maximumNumberOfTouches = 2; 4 _panGestureRecognizer.minimumNumberOfTouches = 2; 5 [self.view addGestureRecognizer:_panGestureRecognizer]; 6 7 - (void)handlerPanGesture:(UIPanGestureRecognizer *)recognizer 8 { 9 if ((recognizer.state == UIGestureRecognizerStateBegan) || 10 (recognizer.state == UIGestureRecognizerStateChanged)) 11 { 12 CGPoint offset = [recognizer translationInView:self.view]; 13 CGRect frame = self.rightViewController.view.frame; 14 frame.origin.x += offset.x; 15 if (frame.origin.x >= 0 && frame.origin.x <= kScreenWidth) 16 { 17 self.rightViewController.view.frame = frame; 18 } 19 20 [recognizer setTranslation:CGPointZero inView:self.view]; 21 } 22 else if (recognizer.state == UIGestureRecognizerStateEnded) 23 { 24 BOOL isVisible = self.rightViewController.view.frame.origin.x < kScreenWidth / 2; 25 [self showRightView:isVisible]; 26 } 27 }
手势识别状态
Gesture recognizers从一个状态转到另一状态(state)。对于每个状态,根据它们是否符合特定条件来决定时候可以移动到下一个状态。它们分析多点触摸。是否识别失败。未能识别手势意味着state 转换失败。UIGestureRecognizerStateFailed。详见UIGestureRecognizerState枚举
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { 2 UIGestureRecognizerStatePossible, // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state 3 4 UIGestureRecognizerStateBegan, // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop 5 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 6 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 7 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 8 9 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 10 11 // 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 12 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 13 };
为view添加多个手势
当一个view添加多个手势时,在缺省情况下,没有为优先执行哪个手势做排序,每次发生不同。不过你可以覆盖默认的行为(使用类方法、委托方法、和子类化覆盖这些)
- 指定一个Gesture recognizers应该在另一个前捕捉。
requireGestureRecognizerToFail: 这个方法就是在作为参数的Gesture recognizer失败以后接受者才发生,否则从不会发生。
[self.panRecognizer requireGestureRecognizerToFail:self.swipeRecognizer];
- 允许2个手势同时操作
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
- 禁止在某一点发生Gesture recognizers
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 2 { 3 if ([touch.view isKindOfClass:[UIControl class]]) 4 { 5 return NO; 6 } 7 8 return YES; 9 }
指定一个单向关系两个手势识别器
想控制两个识别器相互作用,但你需要指定一个单向关系,您可以重写或canPreventGestureRecognizer:或canBePreventedByGestureRecognizer:子类方法。return yes。例如,如果你想要一个旋转的姿态来防止捏动作,但你不想夹手势防止旋转的姿态。例如,你想一个旋转手势阻止一个缩放手势,但你不想一个缩放手势阻止旋转手势,就加入下面代码
[rotationGestureRecognizer canPreventGestureRecognizer:pinchGestureRecognizer];
与其他用户界面控件交互
UIControl子类会覆盖parentView的gesture.例如当用户点击UIButton时,UIButton会接受触摸事件,它的parentView不会接收到.这仅适用于手势识别重叠的默认动作的控制,其中包括:
- 一根手指单击动作:UIButton, UISwitch, UIStepper, UISegmentedControl, and UIPageControl.
- 一根手指擦碰动作:UISlider
- 一根手指拖动动作:UISwitch
包含多点触摸的事件
在iOS中,touch是指一根手指在屏幕上滑动、移动.而手势是指有一根或多根手指,它的类是UITouch对象.多点触摸事件是UIEventTypeTouches
当发生触摸时,会触发下列方法:
- touchesBegan:withEvent :触摸屏幕时发生
- touchesMoved:withEvent :手指在屏幕上移动时发生
- touchesEnded:withEvent :触摸结束时发生,即手指离开屏幕时
- touchesCancelled:withEvent :触摸被系统取消时发生,如有电话时,该触摸会被取消
改变默认的事件传递顺序
你可以通过改变UIGestureRecognizer子类的属性来改变默认的事件传递
- @property(nonatomic) BOOL delaysTouchesBegan; // default is NO. causes all touch events to be delivered to the target view only after this gesture has failed recognition. set to YES to prevent views from processing any touches that may be recognized as part of this gesture
- @property(nonatomic) BOOL delaysTouchesEnded; // default is YES. causes touchesEnded events to be delivered to the target view only after this gesture has failed recognition. this ensures that a touch that is part of the gesture can be cancelled if the gesture is recognized
如果一个gesture recognizer检测到了一个不属于它的touch,它将手势直接发送给它的view,此时会调用ignoreTouch:forEvent: 方法直接传递给view.
创建自定义gesture recognizer步骤
- 创建UIGestureRecognizer的子类
- #import <UIKit/UIGestureRecognizerSubclass.h>
- 在子类冲重载下列方法
1.- (void)reset;
2.- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
3.- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
4.- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
5.- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
一般,Tap、pinch,pan、swipe只是一个简单的单个触摸,它有一定的局限性,所以多点触摸诞生了~为实现多点触摸,首先得做下列事情
- 设置view的属性multipleTouchEnabled = YES(注意了。。。默认值是NO);
- 使用CFDictionaryRef来保存触摸过程的参数
对于使用多点触摸处理事件,你必须频繁地存储以后进行触摸比较的触摸状态。例如,你要比较每个触摸的结束点位置和原始位置,你可以在touchesBegan:withEvent: 方法中获取每个触摸的原始位置(使用locationInView:方法),然后存储在CFDictionaryRef对象中,使用UITouch对象地址作为KEY。然后你可以在touchesEnded:withEvent: 方法中取出原始点,和当前点进行比较。
需要注意的是这里使用CFDictionaryRef对象而不是NSDitionary对象,因为UITouch类没有实现NSCopying协议。
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 2 [self cacheBeginPointForTouches:touches]; 3 } 4 - (void)cacheBeginPointForTouches:(NSSet *)touches { 5 if ([touches count] > 0) { 6 for (UITouch *touch in touches) { 7 CGPoint *point = (CGPoint *)CFDictionaryGetValue(touchBeginPoints, 8 touch); 9 } } 10 if (point == NULL) { 11 point = (CGPoint *)malloc(sizeof(CGPoint)); 12 CFDictionarySetValue(touchBeginPoints, touch, point); 13 } 14 *point = [touch locationInView:view.superview]; 15 }