12

现在,我的 UIPanGestureRecognizer 可以识别每一个平移,这很好且必要,但由于我使用它作为滑动手势来增加和减少变量的值,因此我只想经常执行此方法。如果我每次检测到它时都增加 1,那么值上升得太快了。

有没有办法做这样的事情,每 10 个平移像素执行一次,或类似的事情?

4

1 回答 1

19

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

#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
    }
}
于 2013-04-09T00:58:56.270 回答