I am trying to implement the LongPress Gesture followed by Swipe Gesture on a Button in ios. The view would more likely to be the "Slide to Cancel" and Record feature of WhatsApp application.
Currently i am not receiving swipe gesture event. I am implementing shouldRecognizeSimultaneouslyWithGestureRecognizer method also. Please suggest.
(BOOL)gestureRecognizer:(UIGestureRecognizer *) recognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
We dont need a separate Swipe Gesture Recognizer. The different states in Long Press Gesture Recognizer can be used to handle this scenario.
Long Press Gesture has different states Like UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged and UIGestureRecognizerStateEnded.
UIGestureRecognizerStateBegan gets called as soon as you long press the subview.
UIGestureRecognizerStateChanged gets called when the user tries to move the finger .
UIGestureRecognizerStateEnded gets called when the user lifts the finger from the touch point.
- (void)longPressGestureForPreviewImageView:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
// Long press detected, start the timer
[self showPreviewImage:recognizer];
}
else if(recognizer.state == UIGestureRecognizerStateChanged)
{
NSLog(@"Swipe up");
if ([self.thumbnailImageView.gestureRecognizers containsObject:recognizer]) {
[self.thumbnailImageView removeGestureRecognizer:recognizer];
}
}
else if(recognizer.state == UIGestureRecognizerStateEnded)
{
[self hidePreviewImage];
}
So we can use the Gesture Delegate methods to handle the swipe along with a Long Press Gesture Recognizer.