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

我只是一个虾纸丫 提交于 2019-12-21 03:42:11

问题


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?


回答1:


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
    }
}


来源:https://stackoverflow.com/questions/15888276/with-uipangesturerecognizer-is-there-a-way-to-only-act-so-often-like-after-x-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!