Search code examples
iosuigesturerecognizergestureuipangesturerecognizersimultaneous

2 various UIPanGestureRecognizers with different directions simultaneously?


It is possible to pan up, down, left and right.

Recognizer 1 handles vertical pans only, recognizer 2 handles horizontal pans only. Both of them are linked with the same one delegate object.

Recognizer 2 is fully independent from the first one (except of only one direction from the described 4 ones may be chosen).

So how to prevent one of them from being executed simultaneously with another one?


Solution

  • -(void)initPanGestures{
    UIPanGestureRecognizer *panGestureRecognizer1 = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                    action:@selector(handlePan1Gesture:)];
    
    UIPanGestureRecognizer *panGestureRecognizer2 = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                            action:@selector(handlePan2Gesture:)]
    }
    
    -(void)handlePan1Gesture:(UIPanGestureRecognizer *)sender{
    CGPoint velocity = [sender velocityInView:sender.view];
    
    switch(sender.state) {
    
            case UIGestureRecognizerStateBegan:
            if(fabs(velocity.y) > fabs(velocity.x)){
                panGestureRecognizer2.enabled=YES;
                panGestureRecognizer1.enabled=NO;
            }
            break;
            case UIGestureRecognizerStateCancelled:
             panGestureRecognizer1.enabled=YES;
            break;
        case UIGestureRecognizerStateEnded:{
            panGestureRecognizer1.enabled=YES;
        }
    
            break;
    }
    
    if(velocity.x > 0)
    {
        NSLog(@"gesture went right");
    }
    else if(velocity.x < 0)
    {
        NSLog(@"gesture went left");
    }
    
    
    }
    
    -(void)handlePan2Gesture:(UIPanGestureRecognizer *)sender{
     CGPoint velocity = [sender velocityInView:sender.view];
    switch(sender.state) {
    
        case UIGestureRecognizerStateBegan:
            if(fabs( velocity.x) > fabs(velocity.y)){
                panGestureRecognizer2.enabled=NO;
                panGestureRecognizer1.enabled=YES;
    
            }
            break;
    
        case UIGestureRecognizerStateCancelled:
            panGestureRecognizer2.enabled=YES;
            break;
        case UIGestureRecognizerStateEnded:
             panGestureRecognizer2.enabled=YES;
            break;
    
    }
    
    
    if(velocity.y > 0)
    {
        NSLog(@"gesture went down");
    }
    else if(velocity.y < 0)
    {
        NSLog(@"gesture went up");
    }
    
     }
    

    Also For two diff Gesture you have to implement delegate methods and recognise the gestures.Fro example i have used two gesture simultaneously long and pan gesture

    #pragma mark - UIGestureRecognizerDelegate methods
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    return YES;
    }
    
     - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([panGestureRecognizer1 isEqual:gestureRecognizer]) {
    
        return [panGestureRecognizer2 isEqual:otherGestureRecognizer];
    }
    
    if ([panGestureRecognizer2 isEqual:gestureRecognizer]) {
    
        return [panGestureRecognizer1 isEqual:otherGestureRecognizer];
    }
    
    return NO;
     }