I need to closely monitor the scale of the scroll view so that I can update the content view's elements (a subview managing multiple CALayers) according to the scroll view's animated zoom.
On iOS 3.1, everything works as expected, I use zoomToRect:animated: and the scrollViewDidScroll: message of the UIScrollViewDelegate gets called repeatedly while the animation takes place, letting me update the subview elements according to actual zoom.
The same code on iOS 4.0 does not exibit the same behavior. When I call zoomToRect:animated:, the delegates (scrollViewDidScroll: and scrollViewDidZoom) only get called once, which makes my sub elements desinchronized until the animation is finished. In fact, the sub elements immediately jump and then get caught up by the zoom animation until everything is in the correct place. It's as if the animation is not picking up the modifications on the subviews CALayers.
I have tried animating manually with animation blocks, but the situation is the same, no progressive callback calls. I have also tried KVO, but it is not clear to me how I would tap into a UIScrollView-managed animation.
Is there a workaround on iOS 4 that would allow me to push the scale value to my subviews as the UIScrollView scale is animated?
Brutal but works.
Define some properties:
Inform
UIScrollView
whichUIView
is going to be zoomed:Register for a
CADisplayLink
ticks. It runs Core Animation as well, so it will be kicked on same intervals:Check zoomedView's
presentationLayer
for current values.Register for KVO of the
zoomScale
property of theUIScrollView
after you callzoomToRect:animated:
.Then, implement this method:
Check out the KVO documentation.
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:
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.
See also the answers to this question: How to make UIScrollView send scrollViewDidScroll messages during animations
Is it viable to update all the subviews within an animation block (or begin/commitAnimation)? Something like: