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?
I want to add a reference to this topic. If you need a customized property. This question will help you if completely understanding what's going on. How can I animate a UIColor in a CALayer?
If you figure it out, you can implement
fractionComplete: CGFloat very easily.
But first you have to understand how the animation/action/delegates goes on. All answers are there but it takes time to understand.
It was a long story to handle all the situations. Just as you said, it is very convenient but left to diligent guys.
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:
- frame
- bounds
- center
- transform
- alpha
- backgroundColor
- contentStretch
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:
- Use a completion block
- Implement the CAAnimationDelegate
The list of what can be animated is a lot longer than for UIViewPropertyAnimator. You can find it here.
UIViewPropertyAnimator
or synchronize separate animations with it. –
Earwax 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.
- The Tip: Quartz
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.
© 2022 - 2024 — McMap. All rights reserved.
UIViewPropertyAnimator
. Bonus points for a code example! – Earwax