如何确定平移手势的真实结束速度? [英] How to determine true end velocity of pan gesture?

查看:101
本文介绍了如何确定平移手势的真实结束速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 UIPanGestureRecognizer 并检测 UIGestureRecognizerStateEnded 时,手势的速度不是真正的速度。相反,它是我之前调用我的动作方法的旧速度。如何在手势结束时访问真实的速度?

When using UIPanGestureRecognizer and detecting UIGestureRecognizerStateEnded, then the velocity of the gesture is not the true velocity. Instead, it's the old velocity of the previous invocation of my action method. How can I access the true velocity at the end of the gesture?

我创建我的 UIPanGestureRecognizer ,如下所示:

I create my UIPanGestureRecognizer like this:

    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];

我的行动方法的开头就在这里:

The beginning of my action method is here:

- (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秒后肯定不会报告速度。

As you can see, the velocity is the same in both log entries (same story with translation, but I care only about velocity), although I was holding down my finger for about 30 seconds without moving it, and then lifting the finger. You can tell the timing from the timestampts of the entries. There should certainly not be a velocity reported after 30 seconds of not moving my finger.

我已经使用适用于iOS 6.1的iPhone的iOS模拟器进行了测试。

I've tested this with the iOS simulator for iPhone with iOS 6.1.

推荐答案

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

The velocityInView method is defined only when a pan occurs. That is, only when you're actually moving the finger a pan gesture is occurring. If you keep your finger still, it does not actually trigger a pan gesture.

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

This means that there is no built-in method to know the movement speed when the gesture ends. You could do something like check the time difference between the last event with the status value as UIGestureRecognizerStateChanged and UIGestureRecognizerStateEnded. You can then tune this threshold in order to obtain the desired behavior.

例如

- (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;
    }   
 }

这篇关于如何确定平移手势的真实结束速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆