Ios – The zoomScale property of UIScrollView isn’t updated while zoom-bouncing

iosiphoneobjective cuiscrollview

Right now I have a view that is outside of the UIScrollView. For many reasons, it cannot be simply inside the scrollview. So right now what i'm doing is, as the UIScrollView is scaled/panned, I update my other auxiliary view. The only issue i'm coming into is, the zoomScale value is not adjusted for while the view is bouncing back to either the minimum or maximum scale. Any suggestions on how to obtain this value while it animates back?

Sample code:

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, appSize.width, appSize.height)];
self.scrollView.contentSize = CGSizeMake(appSize.width * 2, appSize.height * 2);

self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.scrollView.delegate = self;
self.scrollView.bounces = YES;
self.scrollView.bouncesZoom = YES;
self.scrollView.alwaysBounceHorizontal = YES;
self.scrollView.alwaysBounceVertical = YES;
self.scrollView.showsHorizontalScrollIndicator = YES;
self.scrollView.showsVerticalScrollIndicator = YES;

self.scrollViewZoomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,
                                                                   appSize.width * 2,
                                                                   appSize.height * 2)];
[[self scrollView] addSubview:self.scrollViewZoomView];

float minZoomScale = 2.0f;

self.scrollView.minimumZoomScale = minZoomScale;
self.scrollView.maximumZoomScale = 1.0f;
self.scrollView.zoomScale = self.scrollView.minimumZoomScale;

and then on self

-(void) scrollViewDidZoom:(UIScrollView *)scrollView {
    float scale = [scrollView zoomScale];
    [self updateAuxViewScale:scale];
}

Best Answer

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];
    }
}
Related Topic