Rui Lopes
Rui Lopes

Reputation: 2562

UILongPressGestureRecognizer on UITableViewCell - double call

I'm using the UILongPressGestureRecognizer in a cell. What I need is: when a user taps a cell for 1.0 seconds, call one view controller. If the user taps the cell, another VC.

I can accomplish that by using the UILongPressGestureRecognizer. But the issue is that is calls the viewController twice.

Code:

if (indexPath.section == 0 && indexPath.row == 1){
    UILongPressGestureRecognizer *longPressTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(memberListWithSearchOptions)];

    longPressTap.minimumPressDuration = 1.0;

    [cell addGestureRecognizer:longPressTap];
    [longPressTap release];
}

I think that what I need is, after recognizing the LongPress, disable the recognizer, until the tableView appears again on screen.

How can I do that?

Thanks,

RL

Upvotes: 4

Views: 3446

Answers (2)

sinh99
sinh99

Reputation: 3979

You have to check state as below

-  (void)memberListWithSearchOptions:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
    NSLog(@"UIGestureRecognizerStateEnded");
    //Do Whatever You want on End of Gesture
}
else if (sender.state == UIGestureRecognizerStateBegan){
    NSLog(@"UIGestureRecognizerStateBegan.");
    //Do Whatever You want on Began of Gesture
}

Upvotes: -1

user467105
user467105

Reputation:

Instead of disabling it, what you probably need to do is check the gesture recognizer's state property and only display the next view controller if the state is UIGestureRecognizerStateBegan (or UIGestureRecognizerStateEnded).

You'll need to change your method to accept the gesture recognizer as a parameter (and also update the @selector parameter) and check it's state:

UILongPressGestureRecognizer *longPressTap = 
    [[UILongPressGestureRecognizer alloc] initWithTarget:self 
        action:@selector(memberListWithSearchOptions:)];  //colon at end

//...

- (void)memberListWithSearchOptions:(UILongPressGestureRecognizer *)lpt
{
    if (lpt.state == UIGestureRecognizerStateBegan)
        //or check for UIGestureRecognizerStateEnded instead
    {
        //display view controller...
    }
}

Upvotes: 8

Related Questions