Search code examples
iosiphoneuiswipegesturerecognizeruilongpressgesturerecogni

How to initiate a gesture recognizer when a gesture is already active?


I have a Subview A(self.thumbnailImageView) added to my superview. I have added a UILongPressGestureRecognizer and UISwipeGestureRecognizer to my subview A.

[self addLongPressGestureRecognizerForPreviewCell:self.thumbnailImageView];

[self addSwipeUpGestureRecognizerForImageView:self.thumbnailImageView];

Now in the handler methods, when the UILongPressGestureRecognizer state begins, i add a Subview B(bigPreviewImage) to my superview(self.view).

-(void)tapGesture:(UILongPressGestureRecognizer *)recognizer{

 if (recognizer.state == UIGestureRecognizerStateBegan)
 {
    // Long press detected, start the timer
    [self showPreviewImage:recognizer];
 }
 else if(recognizer.state == UIGestureRecognizerStateEnded)
 {
    [self hidePreviewImage];
 }
}

-(void)showPreviewImage:(UILongPressGestureRecognizer *)recognizer{

    UIImageView *bigPreviewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Desktop"]];

    bigPreviewImage.frame = CGRectMake(self.thumbnailImageView.frame.origin.x - 50.0, self.thumbnailImageView.frame.origin.y + self.thumbnailImageView.frame.size.height + 10.0, 300.0, 250.0);
    bigPreviewImage.tag = 10000;//200.0 & 125.0
    [bigPreviewImage setUserInteractionEnabled:YES];

    [self.view addSubview:bigPreviewImage];
} 

Now once the UILongPressGestureRecognizer is active and the user with his finger still pressing the Subview A, If the user swipes up the screen, i want the SwipeGestureRecognizer to get initiated. But the same is not happening. How to initiate a gesture recognizer when a gesture is already active?

I have implemented the shouldRecognizeSimultaneouslyWithGestureRecognizer method but still the swipe up gesture method is not called. Please let me know if i am missing something.


Solution

  • Got it!!!

    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.