PDA

View Full Version : Zoom bounce effect




nashyo
Sep 9, 2012, 10:24 AM
The following zooms to scale perfectly, but no bounce effect like UIScrollView delegate. Any ideas on how to manually create the bounce effect?

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



Reason077
Sep 10, 2012, 09:18 AM
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.