【问题标题】:iphone - double tap fail safe wayiphone - 双击故障安全方式
【发布时间】:2010-04-14 12:18:19
【问题描述】:

我正在尝试检测视图上的双击,但是当双击到来时,第一次点击会触发 TouchesBegan 上的操作,因此,在检测到双击之前,总是首先检测到单击。

如何以仅检测到双击的方式进行此操作?

我不能使用 OS 3.x 手势,因为我必须让它与旧的 OS 版本兼容。

谢谢

【问题讨论】:

    标签: iphone iphone-sdk-3.0


    【解决方案1】:

    部分摘自 scrollViewSuite 示例代码的 tapZoom 示例:

    首先,触摸结束后启动的功能:

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [touches anyObject];
    
        if ([touch tapCount] == 1) {
    
                [self performSelector: @selector(handleSingleTap)
                           withObject: nil
                           afterDelay: 0.35]; // after 0.35s we call it a single tap
    
        } else if([touch tapCount] == 2) {
    
                [self handleDoubleTap];
        }
    
    }
    

    第二:如果在超时期间发生新的触摸,则拦截消息:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        [NSObject cancelPreviousPerformRequestsWithTarget: self
                                                 selector: @selector(handleSingleTap)
                                                   object: nil];
    }
    

    另请参阅:http://developer.apple.com/iphone/library/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomingByTouch/ZoomingByTouch.html#//apple_ref/doc/uid/TP40008179-CH4-SW1

    这里:(scrollView 套件) http://developer.apple.com/iphone/library/samplecode/ScrollViewSuite/Introduction/Intro.html

    【讨论】:

      【解决方案2】:

      编辑:我错过了你说你不能使用 3.x 手势的观点,所以这是对你问题的无效答案,但我会留下它,以防可以使用 3.x 手势的人从中受益.

      您可以创建两个手势识别器,一个用于单击,一个用于双击:

      UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesOne:)];
      singleTapGesture.cancelsTouchesInView = NO; 
      singleTapGesture.delaysTouchesEnded = NO;
      singleTapGesture.numberOfTouchesRequired = 1; // One finger single tap
      singleTapGesture.numberOfTapsRequired = 1;
      [self.view addGestureRecognizer:singleTapGesture];
      [singleTapGesture release];
      
      UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesTwo:)];
      doubleTapGesture.cancelsTouchesInView = NO; 
      doubleTapGesture.delaysTouchesEnded = NO;
      doubleTapGesture.numberOfTouchesRequired = 1; // One finger double tap
      doubleTapGesture.numberOfTapsRequired = 2;
      [self.view addGestureRecognizer:doubleTapGesture];
      [doubleTapGesture release];
      

      然后,重头戏来了:

      [singleTapGesture requireGestureRecognizerToFail : doubleTapGesture];
      

      最后一行,使您的单击处理程序仅在双击失败时才起作用。因此,您可以在应用程序中同时单击和双击。

      【讨论】:

      • 真的是一拳……同时使用两个手势。[singleTapGesture requireGestureRecognizerToFail : doubleTapGesture];节省了很多时间。
      【解决方案3】:

      你在看 tapCount 吗?例如:

      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
               UITouch *touch = [[event allTouches] anyObject];
               if (touch.tapCount == 2) {
                        //double-tap action here
               }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-09
        • 1970-01-01
        • 2012-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-20
        • 1970-01-01
        相关资源
        最近更新 更多