检测UITableView单元格中的Tap&Hold

JFM*_*tin 7 cocoa-touch gesture ios

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

Sim*_*ker 9

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


Den*_*kem 6

这是从我的应用程序直接解除的代码.您应该将这些方法(以及布尔_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];
}
Run Code Online (Sandbox Code Playgroud)

  • 你永远不应该使用像这样的代码 - > _ cancelTouches = YES; 而不是仅仅使用self.cancelTouches = YES; 并声明属性私有 (7认同)
  • 这句话是什么" - > _"?从来没见过:) (2认同)

Gam*_*int 6

//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");

    }

}
Run Code Online (Sandbox Code Playgroud)