Search code examples
iosuikituigesturerecognizeruipangesturerecognizer

UIPanGestureRecognizer not moving object smoothly


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.


Solution

  • 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];
    }