Extend UIViewPropertyAnimator?
Asked Answered
S

3

14

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?

Study answered 24/10, 2017 at 22:45 Comment(0)
T
2

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.

Thin answered 10/11, 2018 at 15:50 Comment(7)
I think you're on the right track but the answer really could be improved by explaining the key idea of that other answer and adopting it for the context of UIViewPropertyAnimator. Bonus points for a code example!Earwax
The reason is that the code really too long. Unless you do need it, not a good example for practice.Thin
#18969129 This is one way to show the implementation.Thin
There's no code on that page using UIViewPropertyAnimator. Which of the many links there do you mean?Earwax
In one answer, there is an apple example to show how to use the fractionCompleteThin
Sorry but that Apple code doesn't use CALayer at all and is not helpful. I maintain that the answer should explain the idea rather than just linking to the "How can I animate a UIColor in a CALayer?" question.Earwax
The example I refer here already demonstrates itself. from 1 to 100 is telling you the fraction. Those codes can be used. The whole tutorial implys how to achieve the UIView.animation{}. So modify a little to achieve UIViewPropetryView. Everything is there.Thin
P
1

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:

The list of what can be animated is a lot longer than for UIViewPropertyAnimator. You can find it here.

Ptyalin answered 10/11, 2018 at 13:2 Comment(3)
Your answer is very fancy way of saying that it doesn't work. :) But it still doesn't answer the question how to extend UIViewPropertyAnimator or synchronize separate animations with it.Earwax
Well the answer is, use CoreAnimation. There you can animate the properties you want and you can synchronise them. This would be cleaner than trying to somehow synchronise UIViewPropertyAnimator. Unless there is a very strong reason to keep UIViewPropertyAnimator, the point stands, that this is the intended way to go.Crushing
The point is, there might be reasons why to use UIViewPropertyAnimator (e.g. other animations already using it, complex timings etc.) in the first place. Now the question is how to integrate CALayer based properties into it. Saying not to use UIViewPropertyAnimator at all doesn't answer the OPs question.Earwax
H
1

You can extend any class if you want but the ability you are asking for is not easy to reach that much. Because CALayers are not UIViews 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:

QuartzCode

But not for simple keyframe animations like this:

QuartzCode2

Hope it helps.

Hilary answered 15/11, 2018 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.