现在,我的 UIPanGestureRecognizer 可以识别每一个平移,这很好而且必要,但是当我使用它作为滑动手势来增加和减少变量的值时,在该方法中我只想经常执行操作。如果我每次检测到它时就增加 1,那么该值就会上升得太快。
有没有办法做到每 10 个像素的平移执行此操作或类似的操作?
您正在寻找translationInView: http://developer.apple.com/library/ios/documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/instm/UIPanGestureRecognizer/translationInView%3a,它告诉您平移已经前进了多远,并且可以根据您的最小距离进行测试。此解决方案不涵盖您在一个方向上来回移动等于最小距离的情况,但如果这对您的场景很重要,那么添加起来并不难。
#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
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)