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

1

12

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?

Donielle answered 8/4, 2013 at 20:23 Comment(2)
I think you want to set the variable based on how far the user has panned, not once per message.Sauceda
Just a reminder that translationInView gives you the distance FROM THE START of the pan, so it's absolutely easy to use - exactly like using a slider control.Avatar
B
19

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
    }
}
Basically answered 9/4, 2013 at 0:58 Comment(1)
Note too that translationInView gives you the distance FROM THE START OF THE PAN. (It's not the distance of the 'most recent section'.) So, it's incredibly easy to set a value, based on panning. In fact it's exactly like using a slider. It's one line of code .. yourValue = [sender translationInView:self.view].x; and that's it.Avatar

© 2022 - 2024 — McMap. All rights reserved.