views:

98

answers:

1

CODE

I have some code that adds a UILongPressGestureRecognizer gesture recognizer called _recognizer to a subclass of a UITableViewCell called cell:

...
UILongPressGestureRecognizer *_recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellLongPressRecognized:)];
_recognizer.allowableMovement = 20;
_recognizer.minimumPressDuration = 1.0f;
[[cell contentView] addGestureRecognizer:_recognizer];
[_recognizer release];
...

The -cellLongPressRecognized: selector simply logs when the gesture ends:

- (void) cellLongPressRecognized:(id)_sender {
    if (((UILongPressGestureRecognizer *)_sender).state == UIGestureRecognizerStateEnded)
        ALog(@"[MyViewController] -cellLongPressRecognized: gesture ended...");
}

My console shows one log message when I tap, hold and release a cell:

[MyViewController] -cellLongPressRecognized: gesture ended...

So far, so good.

ISSUE

The issue is that the table cell's background stays selected only as long as 1.0 second, the _recognizer.minimumPressDuration property.

If I hold my finger on the device any longer than 1.0 second, the cell's background flips back from the UITableViewCellSelectionStyleBlue selection style to its usual, opaque, non-selected background.

To make sure only gesture-specific code is involved with this issue, I have disabled -tableView:didSelectRowAtIndexPath: while testing.

QUESTION

How do I keep the background selected indefinitely, flipped back only when the "long press" gesture ends?

A: 

I changed my test condition from UIGestureRecognizerStateEnded to UIGestureRecognizerStateBegan and the gesture is timed with the cell selection state change:

- (void) cellLongPressRecognized:(id)_sender {
    if (((UILongPressGestureRecognizer *)_sender).state == UIGestureRecognizerStateBegan)
        ALog(@"[MyViewController] -cellLongPressRecognized: gesture began...");
}

Seems counterintuitive naming the event this way, but that seems to work.

Alex Reynolds