All Articles

Implicit vs. Explicit Animations in iOS

What are implicit animations?

implicit (adj.) – capable of being understood from something else though unexpressed – Merriam Webster

Basics

  • Good for: when you don’t want to write a lot of code and the default timing works in your use case, for example: changing a layer’s opacity from 1.0 to 0.0
  • Implicit animations use default timing and default animation properties Updates the layer object’s data value in the layer tree

How do you use implicit animations?

Just update properties of your layer object. Note: When changing layer objects, the changes are reflected immediately by those layer objects – however, the visual appearance of those objects does not change immediately. Instead, Core Animation uses your changes as a trigger to create and schedule one or more implicit animations for execution.

Example of implicit animation

redSquareLayer.opacity = 0.0

What are explicit animations?

Basics

  • Good for: when you want more customized animations
  • Does not update the data in the layer tree; Explicit animations only produce animations
  • It is recommended to always assign the fromValue of the animation object in an explicit animation
  • At the end of the animation, Core Animation removes the animation object from the layer and redraws the layer using its current data values

How do you use explicit animations?

  • Create the animation object
  • Set the fromValue, toValue, duration, and/or other properties
  • Add the animation to the layer you want to animate
  • Set the actual data value in the layer to the final value

Example of explicit animation

let opacityAnimation = CABasicAnimation(keyPath: "opacity") 
opacityAnimation.fromValue = 1.0
opacityAnimation.toValue = 0.0
opacityAnimation.duration = 3.0
redSquareLayer.add(opacityAnimation, forKey: "opacity")
redSquareLayer.opacity = 0.0 // Set the actual data value in the layer to the final value

General notes on animating