UIPanGestureRecognizer不能平滑地移动对象

我试图使用UIPanGestureRecognizer移动我的对象,但我似乎无法让它顺利移动。 当我朝一个特定的方向移动并改变到相反时,它不会立即作出反应。 我认为这个值可能有问题,因为它在“ – >”方向是正值,而在“ – ”方向是负值。

我的代码如下:

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer { CGPoint pointA = [panRecognizer locationInView:self.view]; CGPoint pointB = [panRecognizer translationInView:self.view]; if(panRecognizer.state == UIGestureRecognizerStateEnded || panRecognizer.state == UIGestureRecognizerStateChanged) { _camera.x += pointB.x * 0.0001f; } } 

有没有人有更合适的方法来解决这个任务?

提前致谢。

您应该重置UIPanGestureRecognizer的翻译值:

 - (void)panDetected:(UIPanGestureRecognizer *)panRecognizer { CGPoint point = [panRecognizer translationInView:self.view]; _camera.x += point.x * 0.0001f; [panRecognizer setTranslation:CGPointZero inView:self.view]; } 

Swift 3:

 func panDetected(recognizer: UIPanGestureRecognizer) { let translation = recognizer.translation(in: recognizer.view) _camera.x += translation.x _camera.y += translation.y recognizer.setTranslation(CGPoint.zero, in: recognizer.view) } 

适用于我,不用0.0001f乘以:)