Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend UIViewPropertyAnimator?

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?

like image 384
GoldenJoe Avatar asked Oct 24 '17 22:10

GoldenJoe


People also ask

What is a uiviewpropertyanimator?

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.

What is property animator in UIView?

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.

How to manage the state of the animation in UIView?

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.

What is uiviewanimationcurve?

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).


2 Answers

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.

like image 154
gebirgsbärbel Avatar answered Sep 29 '22 21:09

gebirgsbärbel


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.

like image 43
Mojtaba Hosseini Avatar answered Sep 29 '22 23:09

Mojtaba Hosseini