使用UIPanGestureRecognizer,有没有办法只经常这样做,比如在x多个像素被淘汰之后? [英] With UIPanGestureRecognizer, is there a way to only act so often, like after x many pixels were panned?

查看:115
本文介绍了使用UIPanGestureRecognizer,有没有办法只经常这样做,比如在x多个像素被淘汰之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的UIPanGestureRecognizer可以识别每个平底锅,这很棒且必要,但是当我使用它作为滑动手势来增加和减少变量的值时,在方法中我只想经常动作。如果我每次检测到它增加1,那么值就会快得多。

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.

有没有办法做这样的事情,每10个像素的平移就可以做到这一点,或类似的东西?

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

推荐答案

您正在寻找translationInView:,它告诉你平底锅已经进展了多远,并且可以针对您的最小距离进行测试。此解决方案不包括您在一个方向上来回移动的情况,其数量等于最小距离,但如果这对您的方案很重要,则添加起来并不太难。

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

这篇关于使用UIPanGestureRecognizer,有没有办法只经常这样做,比如在x多个像素被淘汰之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆