开发者

UIScrollView not calling layoutSubviews while zooming on iOS 4.0

I've read many of the UIScrollView questions asked on here but none of them answered this problem.

I 开发者_运维技巧have a UIScrollView that zooms a view containing other subviews. When a zoom is happening, I need to have the subviews of the view move to certain places. I can accomplish this by changing their frames accordingly in the layoutSubviews method of the UIScrollView.

On iOS 3.2 and lower, this works great both for finger-zooming and pragmatic zooming using zoomToRect:animated: method.

On iOS 4.0 or higher, this only works great for finger-zooming. With pragmatic zooming using zoomToRect:animated:, a call to layoutSubviews only happens once (at the end of the zoom). I need this method to get called many times during the duration of the zoom so that my subviews can have smooth position changes along with the zoom.

So in summary, on iOS 3.2, a call to layoutSubviews happens many times and on iOS 4.0 a call to layoutSubviews happens only once when you call zoomToRect:animated:. Does anyone know of a solution to my problem?


In IOS 4.0, animations are done by the OS - I assume to make use of GPU based hardware acceleration as much as possible. As a disadvantage of that, it becomes harder to animate a values that is derived from another (animated) value. As in your case, the positions of the subviews that depend on the zoom level of the UIScrollView. In order to make that happen, you should setup the animation of the subviews to go in parallel with the animation of the zooming. Try something like:

[UIView animateWithDuration:0.5 delay:0
                    options:UIViewAnimationOptionLayoutSubviews
                 animations:^{
    theScrollView.zoomScale = zoomScale;
    [theScrollView layoutSubViews];
} completion:^(BOOL finished) {}];

This should set the frame properties of the subviews from within the same animation context, and therefore, they should be animated together by the OS. I am not sure whether the explicit layoutSubViews is needed, or whether the UIViewAnimationOptionLayoutSubviews option already achieves that.

See also the answers to this question: How to make UIScrollView send scrollViewDidScroll messages during animations

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜