【发布时间】:2013-09-30 10:51:59
【问题描述】:
当使用UIPanGestureRecognizer并检测UIGestureRecognizerStateEnded时,手势的速度不是真实的速度。相反,它是之前调用我的操作方法的旧速度。如何在手势结束时获取真实速度?
我这样创建UIPanGestureRecognizer:
UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
[panGestureRecognizer setMaximumNumberOfTouches:2];
[panGestureRecognizer setMinimumNumberOfTouches:1];
[panGestureRecognizer setDelegate:self];
[panGestureRecognizer setDelaysTouchesBegan:NO];
[panGestureRecognizer setDelaysTouchesEnded:NO];
[panGestureRecognizer setCancelsTouchesInView:NO];
[self addGestureRecognizer:panGestureRecognizer];
我的动作方法的开始在这里:
- (IBAction) panGestureRecognized:(UIPanGestureRecognizer *)recognizer {
UIGestureRecognizerState state = recognizer.state;
CGPoint gestureTranslation = [recognizer translationInView:self];
CGPoint gestureVelocity = [recognizer velocityInView:self];
[CBAppDelegate log:@"panGestureRecognized: state: %s\n translation: (%f, %f)\n velocity: (%f, %f)", [self toString:state], gestureTranslation.x, gestureTranslation.y, gestureVelocity.x, gestureVelocity.y];
日志输出示例:
2013-09-30_10:46:32.830 panGestureRecognized: state: UIGestureRecognizerStateChanged
translation: (-283.000000, 2.000000)
velocity: (-43.046783, 45.551472)
2013-09-30_10:47:02.942 panGestureRecognized: state: UIGestureRecognizerStateEnded
translation: (-283.000000, 2.000000)
velocity: (-43.046783, 45.551472)
如您所见,两个日志条目中的速度相同(翻译相同,但我只关心速度),尽管我按住手指大约 30 秒没有移动它,然后抬起手指手指。您可以从条目的时间戳中判断时间。在我的手指不动 30 秒后肯定不会报告速度。
我已经用 iOS 6.1 的 iPhone 的 iOS 模拟器对此进行了测试。
【问题讨论】:
-
您可以使用时间戳自己计算。对于长期持有,如果起始时间戳足够长,您可以重置起始时间戳,并计算您想要的任何内容。祝你好运!
-
五年来没人回答这个问题真是太神奇了!我已经提出了更好、更正确、更现代的答案.. 2019
标签: iphone ios uipangesturerecognizer