UIPanGestureRecognizer not moving object smoothly

一个人想着一个人 提交于 2019-12-08 05:44:36

问题


Im trying to use UIPanGestureRecognizer to move my object around, but i cannot seem to get it moving smoothly. When i move in a particular direction and change to the opposite it does not react instantly. I assume that there might be something wrong with the value, since it is positive in the "->" direction and negative in "<-".

My code is following:

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

Does anybody have a more proper way to solve this task?

Thanks in advance.


回答1:


You should reset the translation value of UIPanGestureRecognizer:

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



回答2:


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)
}

Works good for me without multiplying with 0.0001f :)



来源:https://stackoverflow.com/questions/9740821/uipangesturerecognizer-not-moving-object-smoothly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!