iphone - iPhone-在iOS 3.x上检测单击和长按

标签 iphone ios cocoa-touch ipad

这是一个针对客户的项目,他希望它使用TouchesBegan / Moved / Ended而不是手势,因为它也必须与iOS 3.0和3.1兼容,并且手势的iOS> = 3.2。

我必须检测自定义元素上的单击和长按,这是一种tableView,带有我们称为单元格的元素。

我必须遵循的规则是:

  • 轻按一下即可在0.1秒后触发一种方法(称其为cellTapped)
  • 如果在执行cellTapped之前滚动了控件,则取消cellTapped执行
  • 如果检测到“长按”,则运行longTap方法而不是cellTapped

  • 这就是我在TouchesBegan中所拥有的
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
      self.isScrolling = NO;
    
      // if single tap detected, fire method within 0.1 seconds
      if ([self elementIsTapped:touches]) {
                 [self performSelector:@selector(cellTapped:)
                       withObject:nil
                       afterDelay:0.1];
      }
    
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    
      if (self.isScrolling == NO) {
            // started scrolling, cancel single tap method that was about to be executed 
            [NSObject cancelPreviousPerformRequestsWithTarget:self 
                                                     selector:@selector(cellTapped:)
                                                       object:nil];
            self.isScrolling = YES;
      }
    }
    

    如何在此逻辑中添加长按检测?谢谢

    最佳答案

    我认为您的逻辑有点倒退。您应该在“触摸开始”方法中为长按添加计时器,而不是点击计时器。然后,在您的touchs end方法中,如果尚未触发该方法,则可以取消该方法,然后调用tap方法;如果已经触发,则不执行任何操作。

    附言iOS 3.0?嗯...也可以写给翻盖手机。

    关于iphone - iPhone-在iOS 3.x上检测单击和长按,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11042714/

    相关文章:

    ios - 当互联网在 iOS 上可用时通知

    iphone - 使用 UIWebView 进行单元测试

    iphone - 我的应用程序可以在新的iOS上使用吗?

    ios - 为什么hitTest :withEvent: called three times for each touch?

    ios - 将 map 旋转到航向位置(无指南针)

    objective-c - 我可以禁用 UIPickerView 滚动声音吗?

    iphone - 无法同时显示2个GLKViewController

    iphone - 转换sql语句以用于核心数据

    iphone - 内存泄漏 - Objective-C

    ios - HTTP Post 适用于 WIFI 但不适用于 Cell