检测Tap&保留在UITableView单元格中

时间:2009-10-27 20:16:37

标签: ios cocoa-touch gesture

我们如何检测水龙头& amp;抓住UITableViewCell

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分: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");

    }

}

答案 2 :(得分:0)

您应该处理 UIControlTouchDown 事件,并根据“hold”的含义,触发NSTimer,该NSTimer将计算自您启动触摸后的间隔,并在触发或释放触摸时无效( UIControlTouchUpInside UIControlTouchUpOutside 事件)。当计时器触发时,您检测到“点击并保持”。