0

我正在编写一个 UIView 子类,它使用该-touches{Began,Moved,Ended,Cancelled}:withEvent:方法来跟踪拖动和点击。(我没有使用平移手势识别器,因为它不能仅识别轻按,而且我不使用轻按和平移识别器,因为它们似乎没有以相同的方式在各个阶段中移动。相信我,我已经尝试了两到三遍。)在拖动结束时,我想检查速度并对我正在执行的操作进行微调,如果他们的手指在屏幕仍在移动时离开了屏幕.

问题是,我的速度计算代码出现了一些奇怪的行为。我的触摸处理方法最终都调用了这个简单的速度计算方法:

- (void)updateDraggingVelocityWithTouch:(UITouch*)touch {
    NSTimeInterval timespan = touch.timestamp - self.lastDraggingTimestamp;

    CGPoint pt = [touch locationInView:self];

    CGPoint v;
    v.x = (pt.x - self.lastDraggingLocation.x)/timespan;
    v.y = (pt.y - self.lastDraggingLocation.y)/timespan;
    self.draggingVelocity = v;

    NSLog(@"Velocity: %@", NSStringFromCGPoint(v));

    self.lastDraggingLocation = pt;
    self.lastDraggingTimestamp = touch.timestamp;
}

但是,由于某种原因,这似乎计算了平底锅末端的异常高速度,正是我想知道速度的时候。例如:

2012-11-10 20:38:02.603 Alef[32850:907] Velocity: {0, 0}
2012-11-10 20:38:02.747 Alef[32850:907] Velocity: {-8.35417, 5.0125}
2012-11-10 20:38:02.763 Alef[32850:907] Velocity: {-30.2068, 0}
2012-11-10 20:38:02.780 Alef[32850:907] Velocity: {-29.7612, 0}
2012-11-10 20:38:02.796 Alef[32850:907] Velocity: {-30.2924, 0}
2012-11-10 20:38:02.863 Alef[32850:907] Velocity: {-7.48297, 0}
2012-11-10 20:38:02.930 Alef[32850:907] Velocity: {-7.526, 0}
2012-11-10 20:38:03.030 Alef[32850:907] Velocity: {-4.99497, 0}
2012-11-10 20:38:03.432 Alef[32850:907] Velocity: {-40.5009, 40.5009}

有时它甚至更高——我已经看到在滑动时结束速度超过 100,否则从未达到 15。

知道为什么会发生这种情况吗?我应该使用与我在这里展示的简单差异不同的算法吗?

4

0 回答 0