1
votes

Is there a way to observe the "time progress" of UIView.animateWithDuration...family of methods from UIView /alternatively CA animations?

I am animating a view's frame and I need to be informed how it is progressing. My line of thinking was I can either

1) tap into CAAnimation related stuff or

2) observe the animated properties (like frame) and do my own calculations each screen frame.

Approach 1) turns out to be a dead end, inspecting the internal of how CAAnimations work told me absolutely nothing...and 2) is flawed as the "model layer tree is updated immediately and tapping into the presentation tree is difficult as the presentation layer is nil when you start.

I am pretty desperate, I was thinking that hooking into CADisplayLink will give me a tick and then I simply check something that is affected by the animation but there is nothing to tap to. Do you think going the NSTimer way that is launched in the same scope as the animation method is ok? If I know animation duration then I can generate the progress myself.

1
The CADisplayLink approach is a good one. Just examine the presentationLayer of the layer being animated. While the layer is updated to its final position, the presentationLayer contains the layer properties mid-flight.Rob
Can you give a concrete example?KudoCC
See stackoverflow.com/a/22727484/1271826 or stackoverflow.com/a/21659736/1271826 or stackoverflow.com/a/15367737/1271826. Or just search for "presentationLayer CADisplayLink".Rob

1 Answers

0
votes

If all you want is the time value, then you can do math on the CACurrentMediaTime() minus the animation start time. I have a sample project on Github called KeyframeViewAnimations that does exactly that.

That project supports pausing and resuming and scrubbing both UIView and CAAnimation based animations. In both cases it digs into the underlying CAAnimations.

I have another project that uses the values of the animated layer's presentationLayer in order to do hit testing so you can tap on an in-flight view and start/pause the animation. That one can be found here: iOS-CAAnimation-group-demo

My code uses an NSTimer to update the progress of the animation. It would be better to use a CADisplayLink timer, as you mentioned.

I am also looking at the new UIViewPropertyAnimator class that was added to iOS 10. That makes pausing, reversing, and scrubbing UIView animations easy without having to dig into the underlying CAAnimations. See this thread I just posted:

Is there a way to observe changes to fractionComplete in UIViewPropertyAnimator