Question

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

}
Was it helpful?

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;

OTHER TIPS

You either need to use one of these two ways.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    // test if our control subview is on-screen
    if (cell.view.superview != nil) {
        if ([touch.view isDescendantOfView:cell.view]) {
            // we touched our control surface
            return YES; // handle the touch
        }
    }
    return NO; // ignore the touch
}

Here you need to specify the view for which you want the gestureRecognizer.

Or you can also use these lines of code

gesture.cancelsTouchesInView = NO; 
longPressGesture.cancelsTouchesInView = NO;

Hope it will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top