您的位置:首页 > 移动开发 > IOS开发

iOS手势学习笔记

2016-04-14 14:24 447 查看

UIGestureRecognizer介绍

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

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

UIPanGestureRecognizer(拖动)

UIPinchGestureRecognizer(捏合)

UIRotationGestureRecognizer(旋转)

UITapGestureRecognizer(点按)

UILongPressGestureRecognizer(长按)

​UISwipeGestureRecognizer(轻扫)


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

PS:自定义手势时,需要

#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) 中声明,更多方法声明请自行查看

UIGestureRecongnizer的继承关系如下



手势状态枚举如下

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


检测多次轻点

由于ios支持多个点击,点击1下触发一个动作(action1),点击2(action2)下触发一个动作。这些点击之间的是没有联系的。例如你点击了两下屏幕。那么action1就会触发两次,action2就会触发一次。那怎么保证我只想触发一下action2,而不触发action1

[singleTap requireGestureRecognizerToFail:doubleTap]

这样就会在action2失败以后,才会执行action1

手势的代理

UIGestureReconginizerDelegate
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: