IOS开发之手势——UIGestureRecognizer 共存
2016-05-10 10:30
453 查看
在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断。用 UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。UIGestureRecognizer的衍生类別有以下几种:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer
从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。這些手势別在使用上也很简单,只要在使用前定义并添加到对应的视图上即可。
问题來了。有些手势其实是互相关联的,例如 Tap 与 LongPress、Swipe与 Pan,或是 Tap 一次与Tap 兩次。当一個 UIView 同时添加兩个相关联的手势时,到底我这一下手指头按的要算是 Tap 还是 LongPress?如果照預设作法来看,只要「先滿足条件」的就会跳出并呼叫对应方法,举例来说,如果同时注册了 Pan 和 Swipe,只要手指头一移动就会触发 Pan 然后跳出,因而永远都不會发生 Swipe;单点与双点的情形也是一样,永远都只会触发单点,不會有双点。
那么这个问题有解吗?答案是肯定的,UIGestureRecognizer 有个方法叫做requireGestureRecognizerToFail,他可以指定某一个 recognizer,即便自己已经滿足條件了,也不會立刻触发,会等到该指定的 recognizer 确定失败之后才触发。以同时支持单点与双点的手势为例,代码如下:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer
从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。這些手势別在使用上也很简单,只要在使用前定义并添加到对应的视图上即可。
// 定义一个 recognizer, 并加到需要偵測该手势的 UIView 元件上 - (void)viewDidLoad { UISwipeGestureRecognizer* recognizer; // handleSwipeFrom 是偵測到手势,所要呼叫的方法 recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom)]; // 不同的 Recognizer 有不同的实体变数 // 例如 SwipeGesture 可以指定方向 // 而 TapGesture 則可以指定次數 recognizer.direction = UISwipeGestureRecognizerDirectionUp [self.view addGestureRecognizer:recognizer]; [recognizer release]; } - (void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer { // 触发手勢事件后,在这里作些事情 // 底下是刪除手势的方法 [self.view removeGestureRecognizer:recognizer]; }
问题來了。有些手势其实是互相关联的,例如 Tap 与 LongPress、Swipe与 Pan,或是 Tap 一次与Tap 兩次。当一個 UIView 同时添加兩个相关联的手势时,到底我这一下手指头按的要算是 Tap 还是 LongPress?如果照預设作法来看,只要「先滿足条件」的就会跳出并呼叫对应方法,举例来说,如果同时注册了 Pan 和 Swipe,只要手指头一移动就会触发 Pan 然后跳出,因而永远都不會发生 Swipe;单点与双点的情形也是一样,永远都只会触发单点,不會有双点。
那么这个问题有解吗?答案是肯定的,UIGestureRecognizer 有个方法叫做requireGestureRecognizerToFail,他可以指定某一个 recognizer,即便自己已经滿足條件了,也不會立刻触发,会等到该指定的 recognizer 确定失败之后才触发。以同时支持单点与双点的手势为例,代码如下:
- (void)viewDidLoad { // 单击的 Recognizer UITapGestureRecognizer* singleRecognizer; singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSingleTapFrom)]; singleTapRecognizer.numberOfTapsRequired = 1; // 单击 [self.view addGestureRecognizer:singleRecognizer]; // 双击的 Recognizer UITapGestureRecognizer* double; doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleDoubleTapFrom)]; doubleTapRecognizer.numberOfTapsRequired = 2; // 双击 [self.view addGestureRecognizer:doubleRecognizer]; // 关键在这一行,如果双击确定偵測失败才會触发单击 [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer]; [singleRecognizer release]; [doubleRecognizer release]; }
相关文章推荐
- 【ios学习记录】- UITextField输入浮点数的限制
- TextView 上下默认有padding
- targetSdkVersion 23以下6.0中调用requestPermissions()的问题
- hust Integer Inquiry(字符串,大数相加)
- iOS quartzCore第五章——CAShapeLayer画各种图形 结合UIBezierPath
- 有关easyui-layout中的收缩层无法显示标题的解决办法
- tableview的dequeueResuableCellWithIdentifier会调用initWithStyle
- 清空保存UI控件的数组
- Field 'id' doesn't have a default value 这个错误完美解决
- UUID组成
- iOS UITextField实时监听获取输入内容,中文状态去除预输入拼音
- cell的init会调用内部的initWithStyle,ui控件init最终会调用initWithFrame
- poj1503 Integer Inquiry (大数相加)
- Arduino uno + mpu6050 陀螺仪 运用卡尔曼滤波姿态解算实验
- Handler Looper MessageQueue源码解析
- 第12张 Touch Events and UIResponder翻译
- OpenWRT开发--Build SDK
- Unix Programming - compile apue
- Guest CPU model configuration in libvirt with QEMU/KVM
- MySQL中Packet for query is too large (1327736 > 1048576)错误