Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With UIPanGestureRecognizer, is there a way to only act so often, like after x many pixels were panned?

Right now my UIPanGestureRecognizer recognizes every single pan, which is great and necessary, but as I'm using it as a sliding gesture to increase and decrease a variable's value, within the method I only want to act every so often. If I increment by even 1 every time it's detected the value goes up far too fast.

Is there a way to do something like, every 10 pixels of panning do this, or something similar?

like image 440
user212541 Avatar asked Apr 08 '13 20:04

user212541


1 Answers

You're looking for translationInView:, which tells you how far the pan has progressed and can be tested against your minimum distance. This solution doesn't cover the case where you go back and forth in one direction in an amount equal to the minimum distance, but if that's important for your scenario it's not too hard to add.

#define kMinimumPanDistance 100.0f

UIPanGestureRecognizer *recognizer;
CGPoint lastRecognizedInterval;

- (void)viewDidLoad {
    [super viewDidLoad];

    recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizePan:)];
    [self.view addGestureRecognizer:recognizer];
}

- (void)didRecognizePan:(UIPanGestureRecognizer*)sender {
    CGPoint thisInterval = [recognizer translationInView:self.view];

    if (abs(lastRecognizedInterval.x - thisInterval.x) > kMinimumPanDistance ||
        abs(lastRecognizedInterval.y - thisInterval.y) > kMinimumPanDistance) {

        lastRecognizedInterval = thisInterval;

        // you would add your method call here
    }
}
like image 100
jszumski Avatar answered Nov 15 '22 17:11

jszumski