【问题标题】:Detecting Tap & Hold in UITableView cells检测 UITableView 单元格中的点击并按住
【发布时间】:2009-10-27 20:16:36
【问题描述】:

我们如何检测对 UITableViewCell 的点击并按住?

【问题讨论】:

    标签: ios cocoa-touch gesture


    【解决方案1】:

    在 iOS 3.2 或更高版本中,您可以使用UILongPressGestureRecognizer

    【讨论】:

      【解决方案2】:

      这是直接从我的应用中提取的代码。您应该将这些方法(和一个布尔型 _cancelTouches 成员)添加到从 UITableViewCell 派生的类中。

      -(void) tapNHoldFired {
          self->_cancelTouches = YES;
         // DO WHATEVER YOU LIKE HERE!!!
      }
      -(void) cancelTapNHold {
          [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tapNHoldFired) object:nil];
      }
      
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
          self->_cancelTouches = NO;
          [super touchesBegan:touches withEvent:event];
          [self performSelector:@selector(tapNHoldFired) withObject:nil afterDelay:.7];
      }
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
          [self cancelTapNHold];
          if (self->_cancelTouches)
              return;
          [super touchesEnded:touches withEvent:event];
      }
      
      - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
          [self cancelTapNHold];
          [super touchesMoved:touches withEvent:event];
      }
      
      - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
          [self cancelTapNHold];
          [super touchesCancelled:touches withEvent:event];
      }
      

      【讨论】:

      • 你不应该使用这样的代码 self->_cancelTouches = YES;而不是只使用 self.cancelTouches = YES;并声明财产私有
      • 这个语法“->_”是什么?以前没见过:)
      【解决方案3】:
      //Add  gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView):
      
          // Add long tap for the main tiles
          UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
          [tile addGestureRecognizer:longPressGesture];
          [longPressGesture release];
      
      -(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{
          NSLog(@"gestureRecognizer= %@",gestureRecognizer);
          if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
              NSLog(@"longTap began");
      
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        您可能应该处理 UIControlTouchDown 事件,并根据您所说的“按住”的含义,触发一个 NSTimer,该 NSTimer 将计算自您启动触摸以来的时间间隔,并在触发或释放触摸时失效( UIControlTouchUpInsideUIControlTouchUpOutside 事件)。当计时器触发时,系统会检测到您的“点击并按住”。

        【讨论】:

        • 我没有足够的专家来从这个答案中得出实际代码......但我的意思是在点击并按住 URL 以弹出操作表时在 Mobile Safari 中保持相同的行为最多显示有关此 URL 的选项
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-21
        • 1970-01-01
        • 2021-02-24
        • 1970-01-01
        • 2010-11-05
        • 2011-03-22
        • 2011-05-18
        相关资源
        最近更新 更多