Search code examples
ioscore-animationuipinchgesturerecognizer

Use UIPinchGestureRecognizer to scale layer


How would I use a UIPinchGestureRecognizer to scale a CALayer using a Transform (based off an existing transform, the current state of the layer)?

- (IBAction)gesturePinch:(UIPinchGestureRecognizer *)sender
{
    float scale = sender.scale;

    scale = scale - previousScale;
    previousScale = scale;


    layer.transform  = CATransform3DScale(square.transform, scale, scale, scale);

}

Because the scale is incrementing it gets either huge or very small very quickly. Any suggestions?


Solution

  • It is better to handle the scale like this:

    layer.transform  = CATransform3DScale(square.transform, sender.scale, sender.scale, sender.scale);
    sender.scale = 1.f;
    

    This will reset it every time, making the need to calculate it go away. You will lose the velocity calculations, but I don't think you will be using them anyway.