7

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

4

4 回答 4

9

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

于 2010-12-21T13:28:06.377 回答
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];
}
于 2010-01-28T17:26:29.067 回答
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");

    }

}
于 2011-06-04T04:28:18.203 回答
0

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

于 2009-10-27T21:59:47.277 回答