The zoomScale property of UIScrollView isn't updated while zoom-bouncing The zoomScale property of UIScrollView isn't updated while zoom-bouncing ios ios

The zoomScale property of UIScrollView isn't updated while zoom-bouncing


The bouncing back of the zoom is an animation, so the property zoomScale is immediately the target value of the animation, even if the animation is still running. If you would like to apply the same zoom scale that the scroll view has to another view, here is a way to retrieve it:

  1. In -scrollViewDidScroll: check whether the content view has animations.
  2. If there are no animations, only set the new zoom scale.
  3. If there are animations, get the duration of one of them (the durations should all be equal), and animate your view with that duration to the new zoom scale.

That approach is not elegant, but it works. Here is some code to get you started:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    CGFloat zoomScale = _scrollView.zoomScale;    CALayer *layer = _contentView.layer;    NSArray *animationKeys = [layer animationKeys];    if (animationKeys.count == 0) {        // set zoomScale on target view    } else {        CFTimeInterval duration = 0.0;        for (NSString *animationKey in animationKeys) {            CAAnimation *animation = [layer animationForKey:animationKey];            duration = animation.duration;            break;        }        // easeInEaseOut is the default animation when the zoom bounces        [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{            // set zoomScale on target view        } completion:nil];    }}


Thanks to this answer it becomes clear that one can use the presentationLayer to get the correct values.I created a UIScrollView category to add special getters for zoomScale, contentOffset and contentSize which return the correct values even if the UIScrollView is animating.

You can find the repo here: https://github.com/bddckr/BDDRScrollViewExtensions


You should use a different UIScrollViewDelegate method.

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {    // Your code here    float scale = [scrollView zoomScale];    [self updateAuxViewScale:scale];}

According to the docs: https://developer.apple.com/library/ios/#documentation/uikit/reference/uiscrollviewdelegate_protocol/Reference/UIScrollViewDelegate.html

"The scroll view also calls this method after any “bounce” animations."