为什么使用UIPanGestureRecognizer移动对象时会有延迟

我使用UIPanGestureRecognizer移动UIView对象 – 我在屏幕上拖动手指的方式有多less,我将视图以相同的方向移动(仅在X – 左侧或右侧,Y没有改变)。 它工作正常,但(非常明显)延迟。

以下是处理UIPanGestureRecognizer事件的方法:

-(void)movePages:(UIPanGestureRecognizer *)sender { if(switchingMode == 1){ if([sender state] == UIGestureRecognizerStateBegan){ fingerStartPosition = [sender locationInView:self.view].x; viewStartPosition = [[viewControllers objectAtIndex:activeViewControllerIndex] view].center; } [[[[viewControllers objectAtIndex:activeViewControllerIndex] view] layer] setPosition:CGPointMake(viewStartPosition.x - (fingerStartPosition - [sender locationInView:self.view].x) , viewStartPosition.y)]; } } 

我试图用它的图层设置视图的位置,我也尝试使用不同持续时间的animation设置框架,但是所有的performance都是一样的。 任何想法为什么这个延迟发生?

使用UILongPressGestureRecognizer并将UILongPressGestureRecognizer设置为0.0 。 这可以立即识别,并获得所有相同的更新,包括具有更新位置的UIGestureRecognizerStateChanged

我发现,如果只使用常规的touchesBegan,Moved和Ended,它的响应速度会更快。 我甚至划分了一个UIGestureRecognizer,它仍然在平移手势上滞后。 即使UIGestureRecognizer中的touchesBegin会及时触发,状态改变也需要半秒来改变它的状态……使用一个普通的TouchesBegan似乎更快,尤其是如果你是cpu的时候,做的很多。

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { if touches.count == 1 { initialTouchLocation = (touches.first?.locationInView(self).x)! } } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { if touches.count == 1 { let locationInView = touches.first?.locationInView(self) if !thresholdHit { //this is the threshold for x movement in order to trigger the panning... if abs(initialTouchLocation - locationInView!.x) > 1 { thresholdHit = true } } else { if (self.frame.width != CGFloat(screenSize)) { let panDelta = initialTouchLocation - locationInView!.x } } } } 

如果手势识别器是平移手势,在将手指移动一些像素之前,则无法确定。 我不知道准确的容忍值,但这就是为什么你感觉到延迟。

文档:

平移手势是连续的。 当允许的最小数量的手指移动到足以被认为是平底锅时开始。

如果你想要即时移动,你可能需要使用touchesMoved:构build自己的逻辑touchesMoved:

另一种方法可能是,首先要认识到的一点。 但是这并不能消除延误。 对于这种方法,你可以看看我的JDDroppableView在github上。