When to use UIView, CABasicAnimation, and UIViewPropertyAnimator animations?

I wanted to know in which scenarios should we use any of them as the best option?

On the next blog, https://www.hackingwithswift.com/ios10 , an example written in the Animation Updates section, you can re-apply the same requirement using "CABasicAnimation", which means pausing and resuming the animation?

From what I compiled, when we use the UIView.animate (...) method, it returns a void, so we won’t be able to control the animation until it is complete, because we don’t get the return value to work, because we get the UIViewPropertyAnimator (also, here we have "isRunning" to check the progress of the animation.) Also in CABasicAnimation we don’t have any kind of progress check to run the animation. Please correct me if my assumptions are wrong. Thanks.

+5
source share
1 answer

All of these APIs are great, and they all have slightly different use cases.

UIView animation methods

This, in my opinion, is still the easiest to use. Very direct API without making code too large. I use this either for a simple fire-and-forget animation (for example, when a button is pressed when pressed), or when I want to create a complex animation of a keyframe, as its keyframe supports it.


CAAnimation

Perhaps a little less straightforward, but you need it when you want to animate the properties of the layer , rather than view the properties. Examples of this are angular radius, shadow, and border.


UIViewPropertyAnimator

This is only available with iOS10, so you probably want to revert to one of the previous APIs for older versions of iOS. As the name suggests, this is another presentation-based animation API. What sets it apart from UIView's animation methods is that it supports interactive animations. You can pause, rewind and brighten the animation, which makes it powerful enough. I use this when I want the animation to be controlled by the user. The scrub works by setting the animator's fractionComplete property, which can be easily connected to a gesture recognizer or touch recognition (or even to a key using KVO).

You can also save the link to the UIViewPropertyAnimator and change its animations or completion blocks during the animation.

It should be noted that you can use UIView.animate and UIView.animateKeyframes from your UIViewPropertyAnimator animation blocks if you feel the need to use both.

+12
source

Source: https://habr.com/ru/post/1265474/


All Articles