JavaCake
JavaCake

Reputation: 4115

Pinching (zoom) gesture not scaling correctly

Im trying to figure out why my scaling is acting very strange on my OpenGL camera. Its purpose is it zoom in and out, but its scaling very random.

Heres my code:

- (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
    NSLog (@"%@", @"Pinching");

    if (pinchRecognizer.state == UIGestureRecognizerStateBegan ||
               pinchRecognizer.state == UIGestureRecognizerStateChanged ||
               pinchRecognizer.state == UIGestureRecognizerStateEnded) {
        currentScale = pinchRecognizer.scale;
    }

    if(lastScale < currentScale) {
       _camera.z += currentScale * 0.01f;
       lastScale = currentScale;
    } 

    if (lastScale > currentScale) 
    {
       _camera.z -= currentScale * 0.01f;
       lastScale = currentScale;
    }

}

Upvotes: 0

Views: 494

Answers (1)

yuji
yuji

Reputation: 16725

If you have something that is multiplicative and you want to make it additive, you need to take a log:

- (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
    if ((gesture.state == UIGestureRecognizerStateBegan) ||
        (gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {
        _camera.z += log(gesture.scale); // you'll probably want to multiply this by some constant
        gesture.scale = 1.0;
    }
}

Upvotes: 1

Related Questions