Search code examples
iosobjective-cios7

Locking UIPanGestureRecognizer to a certain direction


I have UIPanGestureRecognizer and I'm trying to get it only swipe in one direction (up). I haven't been able to find a solution that works. Thanks. This is my current code:

- (void)panGesture:(UIPanGestureRecognizer *)recognizer{

CGPoint t = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + t.x, recognizer.view.center.y + t.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

Solution

  • - (void)panGesture:(UIPanGestureRecognizer *)recognizer {
        CGPoint t = [recognizer translationInView:self.view];
    
        if (t.y < 0) {
            t = CGPointMake(0, t.y);
        }
        else {
            t = CGPointMake(0, 0); // look at this
        }
    
        recognizer.view.center = CGPointMake(recognizer.view.center.x + t.x,
                                             recognizer.view.center.y + t.y);
    
        [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    }
    

    To be able to drag up and down use CGPointMake(0, t.y); instead of my if