UIViewPropertyAnimator
is very convenient for modifying a view's transform, alpha, etc. However, there are times when you wish to animate a CAShapeLayer
path, or a UIImageView
image changing. Is there any way to extend the functionality of UIViewPropertyAnimator to support additional properties? If not, how could I synchronize separate animations with a complex keyframe animation?
A class that animates changes to views and allows the dynamic modification of those animations. A UIViewPropertyAnimator object lets you animate changes to views and dynamically modify your animations before they finish.
A UIViewPropertyAnimator object lets you animate changes to views and dynamically modify your animations before they finish. With a property animator, you can run your animations from start to finish normally or you can turn them into interactive animations and control the timing yourself.
Through the UIViewAnimating protocol are implemented features to manage the state of the animation in a simple and clear way, implemented by functions like startAnimation, pauseAnimation and stopAnimation. Calling those functions we update the state value, making it switch between active, inactive and stopped.
As you have probably noticed with the previous example, together with the animation block we have defined two parameters: the duration of the animation and the animation curve, a UIViewAnimationCurve instance that can represents the most common curves (easeIn, easeOut, linear or easeInOut).
UIViewPropertyAnimator
is easy to use. That simplicity comes with severe limitations however. According to the documentation, it can only be used to animate the view directly. This excludes properties of the CAShapeLayer.
So, what can you animate using UIViewPropertyAnimator
? Everything in this list:
If you want to animate the CAShapeLayer
, you have to use CoreAnimation
instead. While more complicated to use, it fortunately also allows you to combine animations.
For example if you want to run two animations at the same time, you can use a CAAnimationGroup
like this:
let fadeOut = CABasicAnimation(keyPath: "opacity")
fadeOut.fromValue = 1
fadeOut.toValue = 0
fadeOut.duration = 1
let expandScale = CABasicAnimation()
expandScale.keyPath = "transform"
expandScale.valueFunction = CAValueFunction(name: kCAValueFunctionScale)
expandScale.fromValue = [1, 1, 1]
expandScale.toValue = [3, 3, 3]
let fadeAndScale = CAAnimationGroup()
fadeAndScale.animations = [fadeOut, expandScale]
fadeAndScale.duration = 1
You can also detect, when an animation finished and use this to start another animation. There are two ways to do this:
The list of what can be animated is a lot longer than for UIViewPropertyAnimator. You can find it here.
You can extend any class if you want but the ability you are asking for is not easy to reach that much. Because CALayer
s are not UIView
s and UIViewAnimation...
are not work on them (P.S: Apple recently added some sort of support for cornerRadius and some other layer properties, but not all of them).
Animation stuff is required you to get to know more about animation logics like framing and keyframes and timeline and etc. Once you learn those enough, you can do complex keyframe animations your own.
There are some tools out there that makes animating a lot easier by their GUI like QuartzCode. I personally use it when super complex animations comes in like this:
But not for simple keyframe animations like this:
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With