Обнаружение касания и удержания в ячейках UITableView - PullRequest
7 голосов
/ 27 октября 2009

Как мы можем обнаружить нажатие и удерживать UITableViewCell?

Ответы [ 4 ]

9 голосов
/ 21 декабря 2010

В iOS 3.2 или более поздней версии вы можете использовать UILongPressGestureRecognizer

6 голосов
/ 04 июня 2011
//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");

    }

}
6 голосов
/ 28 января 2010

Вот код, взятый прямо из моего приложения. Вы должны добавить эти методы (и логический член _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];
}
0 голосов
/ 28 октября 2009

Вам, вероятно, следует обработать событие UIControlTouchDown и, в зависимости от того, что вы подразумеваете под "удержанием", запустить NSTimer, который будет считать интервал с момента инициации касания, и сделать недействительным после запуска или отпускания касания ( UIControlTouchUpInside и UIControlTouchUpOutside события). Когда таймер срабатывает, у вас обнаружен «постучите и удерживайте».

...