gpt4 book ai didi

iphone - 如何确定平移手势的真实最终速度?

转载 作者:可可西里 更新时间:2023-11-01 05:01:32 25 4
gpt4 key购买 nike

当使用 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];

我的 Action 方法的开头在这里:

- (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 模拟器对此进行了测试。

最佳答案

velocityInView 方法仅在平移发生时定义。也就是说,只有当您实际移动手指时,才会出现平移手势。如果您的手指保持不动,它实际上不会触发平移手势。

这意味着没有内置方法可以知道手势结束时的移动速度。您可以执行一些操作,例如检查状态值为 UIGestureRecognizerStateChangedUIGestureRecognizerStateEnded 的最后一个事件之间的时间差。然后您可以调整此阈值以获得所需的行为。

例如

- (IBAction) panGestureRecognized:(UIPanGestureRecognizer *)recognizer {

UIGestureRecognizerState state = recognizer.state;

CGPoint gestureTranslation = [recognizer translationInView:self];
CGPoint gestureVelocity = [recognizer velocityInView:self];

if ( state == UIGestureRecognizerStateChanged )
_lastChange = CFAbsoluteTimeGetCurrent();
else if ( state == UIGestureRecognizerStateEnded ) {
double curTime = CFAbsoluteTimeGetCurrent();
double timeElapsed = curTime - _lastChange;
if ( timeElapsed < MY_THRESHOLD )
finalSpeed = gestureVelocity;
else
finalSpeed = CGPointZero;
}
}

关于iphone - 如何确定平移手势的真实最终速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19092375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com