Question

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.

Was it helpful?

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top