识别单击、双击

  1. 检测手势有两种方法,一种是定制子视图,重写视图从UIResponder类中继承来的事件处理方法,即touchesBegan:withEvent:等一系列方法来检测手势;另一个方法是使用手势识别器,即UIGestureRecognizer的各种具体子类。

    一.重写事件处理方法

    [objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. - (id)init {  
    2.     if ((self = [super init])) {  
    3.         self.userInteractionEnabled = YES;  
    4.         self.multipleTouchEnabled = YES;  
    5.         // ...  
    6.     }  
    7.     return self;  
    8. }  
    9. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
    10. {  
    11.     [NSObject cancelPreviousPerformRequestsWithTarget:self];  
    12.     UITouch *touch = [touches anyObject];  
    13.     CGPoint touchPoint = [touch locationInView:self];  
    14.   
    15.     if (touch.tapCount == 1) {  
    16.         [self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3];  
    17.     }else if(touch.tapCount == 2)  
    18.     {  
    19.         [self handleDoubleTap:[NSValue valueWithCGPoint:touchPoint]];  
    20.     }  
    21. }  
    22.   
    23. -(void)handleSingleTap:(NSValue*)pointValue  
    24. {  
    25.     CGPoint touchPoint = [pointValue CGPointValue];  
    26.     //...  
    27. }  
    28.   
    29. -(void)handleDoubleTap:(NSValue*)pointValue  
    30. {  
    31.     CGPoint touchPoint = [pointValue CGPointValue];  
    32.     //...  
    33. }  

    1. 首先确认定制视图的userInteractionEnabledmultipleTouchEnabled属性都为YES.
    2. 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:方法执行,这样识别出了单击操作。

    3. 如果这是一个双击操作,则第二次点击在0.3s内触发,在第二次触摸操作的touchesEnded:withEvent:方法中,cancelPreviousPerformRequestsWithTarget:首先会取消之前对handleSingleTap:方法的调度,使之不会执行,然后在调用handleDoubleTap:方法处理双击操作。

    二.使用Gesture Recognizer

    使用Gesture Recognizer识别就会简单许多,只需添加两个手势识别器,分别检测单击和双击事件,设置必要的属性即可。

    [objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. - (id)init {  
    2.     if ((self = [super init])) {  
    3.     self.userInteractionEnabled = YES;  
    4.         UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];  
    5.         singleTapGesture.numberOfTapsRequired = 1;  
    6.         singleTapGesture.numberOfTouchesRequired  = 1;  
    7.         [self addGestureRecognizer:singleTapGesture];  
    8.   
    9.         UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];  
    10.         doubleTapGesture.numberOfTapsRequired = 2;  
    11.         doubleTapGesture.numberOfTouchesRequired = 1;  
    12.         [self addGestureRecognizer:doubleTapGesture];  
    13.   
    14.         [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];  
    15.     }  
    16.     return self;  
    17. }  
    18. -(void)handleSingleTap:(UIGestureRecognizer *)sender{  
    19.     CGPoint touchPoint = [sender locationInView:self];  
    20.     //...  
    21. }  
    22. -(void)handleDoubleTap:(UIGestureRecognizer *)sender{  
    23.     CGPoint touchPoint = [sender locationInView:self];  
    24.     //...  
    25. }  

    唯一需要注意的是

        
        
    [objc] view plain copy 在CODE上查看代码片 派生到我的代码片
    1. [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];  

    这句话的意思时,只有当doubleTapGesture识别失败的时候(即识别出这不是双击操作),singleTapGesture才能开始识别,同我们一开始讲的是同一个问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值