现在,我的 UIPanGestureRecognizer 可以识别每一个平移,这很棒而且很有必要,但是当我将它用作滑动手势来增加和减少变量的值时,在方法中我只想每隔一段时间就执行一次。如果我每次检测到它时都增加 1,则该值上升得太快了。
有没有办法做类似每平移 10 个像素或类似的事情?
最佳答案
您正在寻找 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
}
}
关于ios - 使用 UIPanGestureRecognizer,有没有一种方法只能如此频繁地执行操作,比如在平移 x 个像素之后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15888276/