手势操作---识别单击还是双击
2014-03-29 16:38
567 查看
在视图上同时识别单击手势和双击手势的问题在于,当检测到一个单击操作时,无法确定是确实是一个单击操作或者只是双击操作中的第一次点击。解决这个问题的方法就是:在检测到单击时,需要等一段时间等待第二次点击,如果没有第二次点击,则为单击操作;如果有第二次点击,则为双击操作。检测手势有两种方法,一种是定制子视图,重写视图从UIResponder类中继承来的事件处理方法,即touchesBegan:withEvent:等一系列方法来检测手势;另一个方法是使用手势识别器,即UIGestureRecognizer的各种具体子类。
- (id)init {
if ((self = [super init])) {
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
// ...
}
return self;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
if (touch.tapCount == 1) {
[self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3];
}else if(touch.tapCount == 2)
{
[self handleDoubleTap:[NSValue valueWithCGPoint:touchPoint]];
}
}
-(void)handleSingleTap:(NSValue*)pointValue
{
CGPoint touchPoint = [pointValue CGPointValue];
//...
}
-(void)handleDoubleTap:(NSValue*)pointValue
{
CGPoint touchPoint = [pointValue CGPointValue];
//...
}
首先确认定制视图的userInteractionEnabled和multipleTouchEnabled属性都为YES.
在touchesEnded:withEvent:方法中,如果是第一次触摸结束,则cancelPreviousPerformRequestsWithTarget:方法不会起作用,因为self未调度任何方法,此时tapCount为1,使用performSelector:withObject:afterDelay:调用单击事件处理方法,在0.3s钟后执行。
如果这是一个双击操作,则第二次点击在0.3s内触发,在第二次触摸操作的touchesEnded:withEvent:方法中,cancelPreviousPerformRequestsWithTarget:首先会取消之前对handleSingleTap:方法的调度,使之不会执行,然后在调用handleDoubleTap:方法处理双击操作。
- (id)init {
if ((self = [super init])) {
self.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
singleTapGesture.numberOfTapsRequired = 1;
singleTapGesture.numberOfTouchesRequired = 1;
[self addGestureRecognizer:singleTapGesture];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTapGesture.numberOfTapsRequired = 2;
doubleTapGesture.numberOfTouchesRequired = 1;
[self addGestureRecognizer:doubleTapGesture];
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
}
return self;
}
-(void)handleSingleTap:(UIGestureRecognizer *)sender{
CGPoint touchPoint = [sender locationInView:self];
//...
}
-(void)handleDoubleTap:(UIGestureRecognizer *)sender{
CGPoint touchPoint = [sender locationInView:self];
//...
}
唯一需要注意的是[objc] view plaincopy
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
@这句话的意思时,只有当doubleTapGesture识别失败的时候(即识别出这不是双击操作),singleTapGesture才能开始识别,同我们一开始讲的是同一个问题。
一.重写事件处理方法
[objc] view plaincopy- (id)init {
if ((self = [super init])) {
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
// ...
}
return self;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
if (touch.tapCount == 1) {
[self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3];
}else if(touch.tapCount == 2)
{
[self handleDoubleTap:[NSValue valueWithCGPoint:touchPoint]];
}
}
-(void)handleSingleTap:(NSValue*)pointValue
{
CGPoint touchPoint = [pointValue CGPointValue];
//...
}
-(void)handleDoubleTap:(NSValue*)pointValue
{
CGPoint touchPoint = [pointValue CGPointValue];
//...
}
首先确认定制视图的userInteractionEnabled和multipleTouchEnabled属性都为YES.
在touchesEnded:withEvent:方法中,如果是第一次触摸结束,则cancelPreviousPerformRequestsWithTarget:方法不会起作用,因为self未调度任何方法,此时tapCount为1,使用performSelector:withObject:afterDelay:调用单击事件处理方法,在0.3s钟后执行。
[self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3];如果这是一个单击操作,则后面0.3钟内不会再有触摸事件,则handleSingleTap:方法执行,这样识别出了单击操作。
如果这是一个双击操作,则第二次点击在0.3s内触发,在第二次触摸操作的touchesEnded:withEvent:方法中,cancelPreviousPerformRequestsWithTarget:首先会取消之前对handleSingleTap:方法的调度,使之不会执行,然后在调用handleDoubleTap:方法处理双击操作。
二.使用Gesture Recognizer
使用Gesture Recognizer识别就会简单许多,只需添加两个手势识别器,分别检测单击和双击事件,设置必要的属性即可。[objc] view plaincopy- (id)init {
if ((self = [super init])) {
self.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
singleTapGesture.numberOfTapsRequired = 1;
singleTapGesture.numberOfTouchesRequired = 1;
[self addGestureRecognizer:singleTapGesture];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTapGesture.numberOfTapsRequired = 2;
doubleTapGesture.numberOfTouchesRequired = 1;
[self addGestureRecognizer:doubleTapGesture];
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
}
return self;
}
-(void)handleSingleTap:(UIGestureRecognizer *)sender{
CGPoint touchPoint = [sender locationInView:self];
//...
}
-(void)handleDoubleTap:(UIGestureRecognizer *)sender{
CGPoint touchPoint = [sender locationInView:self];
//...
}
唯一需要注意的是[objc] view plaincopy
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
@这句话的意思时,只有当doubleTapGesture识别失败的时候(即识别出这不是双击操作),singleTapGesture才能开始识别,同我们一开始讲的是同一个问题。
相关文章推荐
- Android 手势识别——单击/双击
- Android 手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
- Android手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
- iOS开发笔记--识别单击还是双击
- IOS学习 触摸和手势 手势识别器:单击、双击、轻扫、滑动、长按、旋转
- Android手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
- 【转】iOS开发笔记--识别单击还是双击
- cocos2d-x2.1.5中实现手势识别(各种形状,单击,双击)
- Android手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
- iOS开发笔记--识别单击还是双击
- Android 手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
- iOS 同一个View识别单击和双击手势
- Hololens入门之手势识别(单击、双击)
- iOS - 同一个View识别单击和双击手势
- iOS开发笔记--识别单击还是双击
- UITapGestureRecognizer手势识别之双击和单击结合
- iOS UITapGestureRecognizer区分单击还是双击
- iphone手势识别(双击、捏、旋转、拖动、划动、长按)
- iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
- iOS常见手势总结-单击、双击、捏合、旋转、移动、清扫、长按