Zoom bounce effect

Discussion in 'iPhone/iPad Programming' started by nashyo, Sep 9, 2012.

  1. macrumors 6502

    nashyo

    #1
    The following zooms to scale perfectly, but no bounce effect like UIScrollView delegate. Any ideas on how to manually create the bounce effect?

    Code:
    -(void)pinchGestureRecognized:(UIPinchGestureRecognizer*)recognizer
    {
            if (recognizer.state == UIGestureRecognizerStateBegan) {
                lastScale = recognizer.scale;
            }
        
            if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
            {
                CGFloat currentScale = [[recognizer.view.layer valueForKeyPath:@"transform.scale"] floatValue];
        
                const CGFloat kMinScale = 0.75;
                const CGFloat kMaxScale = 1.00;
        
                CGFloat newerScale = 1 - (lastScale - recognizer.scale);
                newerScale = MIN(newerScale, kMaxScale/currentScale);
                newerScale = MAX(newerScale, kMinScale/currentScale);
                CGAffineTransform transform = CGAffineTransformScale(recognizer.view.transform, newerScale, newerScale);
                recognizer.view.transform = transform;
        
                lastScale = recognizer.scale;
            }
    }
     
  2. macrumors 68000

    Reason077

    #2
    You can add a bounce effect pretty easily using [UIView animateWithDuration...]. If the user goes over the limit you define, just animate it back to the limit value when the pinch gesture ends.

    If you want to get more advanced, you can also add resistance, etc.
     

Share This Page