UIScrollView with decelerating animation while zooming (same behavior as in scroll)

300 views Asked by At

I'm writing a UIScrollView component that the user interacts with in the same manner as a he does in a map app like google maps or Apple's native map app.

I'm looking to give the user the same experience of scrolling and zooming that he is used to get - i.e - a decelerating effect when scrolling and zooming.

The scrolling part is easy to implement by setting the decelerationRate property of the UIScrollView.

My question is - how can I implement the same effect for the zoom-in zoom-out?

1

There are 1 answers

0
FriedPotato On

There seems to be no existing APIs for inertial zooming. Here's the basic idea of custom implementation. You can tweak around the coefficients.

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    NSLog(@"DidEndZooming %f", scale);
    CGFloat speed = scrollView.pinchGestureRecognizer.velocity;
    NSLog(@"Speed: %f", speed);
    CGFloat targetScale = MIN(kMaximumZoomScale, MAX(1, scale + speed * 1/8));
    NSLog(@"Target: %f", targetScale);
    [scrollView setZoomScale:targetScale animated:YES];
}