1

我试图使用 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;
        }
    }

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

提前致谢。

4

2 回答 2

6

您应该重置 UIPanGestureRecognizer 的翻译值:

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{    
   CGPoint point = [panRecognizer translationInView:self.view];
   _camera.x += point.x * 0.0001f;
   [panRecognizer setTranslation:CGPointZero inView:self.view];
}
于 2012-03-17T01:28:21.063 回答
-1

斯威夫特 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 :)

于 2017-05-19T16:25:41.017 回答