JavaCake
JavaCake

Reputation: 4115

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.

Upvotes: 1

Views: 2814

Answers (2)

Florian Blum
Florian Blum

Reputation: 658

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

Upvotes: -1

Darren
Darren

Reputation: 10129

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

Upvotes: 6

Related Questions