I’m trying to animate a layer at exact moments during a view’s cycle. Let’s say, for instance, the animation is a simple fade in (a CABasicAnimation applied to the opacity key). But I can’t figure how to make sure my animation on a CALayer only starts once I’m sure the containing view (and layer) are visible.
2 example cases:
-
Fade the layer in ONLY after the container view (and super layer of
this layer) are already visible to the user, in response to the
superview’s controller being pushed into a navigation controllers’
stack. That is, ONLY after the animation and viewController->view
setup triggered by [navigationController pushViewController:x
animated:YES]; has completed and the viewController’s view is
already visible. -
Fade the layer in ONLY after the user has scrolled, through a scroll
view with paging, to some page. That is, the user scrolls to page n
(and each page is a subview of a scroll view) and ONLY after the new
page has scrolled to the center of the content view, does the
animation start.
It’s sort of like, starting animations once a “book” page is turned, so as to tell a story on each page.
Makes sense?
I figured out both cases:
For the first case, the animation code needs to be in viewDidAppear: and the key is to enclose the animation in a CATransaction. For some reason, without the explicit transaction the animation doesn’t seem to take.
The second example is pretty similar, only that one would add the CATransaction in the scrollview’s delegate DidEndDecelerating method: