Search code examples
iphoneiosios4uigesturerecognizer

Tap Gesture + Long Press Gesture both not working Together


I want use tap gesture and long press gesture together in a view. But my problem is that I can't able to run tap gesture action on tap. But Long press gesture is working fine.

Here is code snippet.

            UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(ontappLongPressGesture:)];
            longPressGesture.minimumPressDuration=0.6;

            longPressGesture.delegate=self;
            [cell.view addGestureRecognizer:longPressGesture];

            UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellSelected:)];
            //[gesture requireGestureRecognizerToFail:longPressGesture]; //I have tried with this line also but not working
            gesture.delegate=self;
            [cell.view addGestureRecognizer:gesture];

Also I have set delegate method also

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

This method is getting called on long press

- (void)ontappLongPressGesture:(id)sender{

    //Long press code here

}

But this method is not getting called on tap

-(void)cellSelected:(id)sender {

     //Single tap code here

}

Solution

  • You haven't specified what type of view your putting these gestureRecognizer's on, however since you are calling it "cell", I'm assuming its on a UITableView?

    You need to make sure you set the cancelsTouchesInView flag if so:

    gesture.cancelsTouchesInView=NO;