Skip to content
tezvyn:

CAKeyframeAnimation: Animating Through Key States

Source: developer.apple.comHardHow cards are made

CAKeyframeAnimation: Animating Through Key States

CAKeyframeAnimation defines animations with waypoints, not just a start and end. Think of it as a flipbook where you set key frames and the system interpolates. Use it for complex paths, like shaking an icon. Footgun: setting both path and values.

Why it exists

Basic animations (CABasicAnimation) are great for moving from state A to state B. But for more complex motion, like an icon shaking or an object tracing a curve, you need to define intermediate points. CAKeyframeAnimation was created to solve this by letting you specify a sequence of states or a path for an animation to follow.

The mental model

Think of CAKeyframeAnimation like a flipbook. Instead of just the first and last page, you draw several key pages in between. Core Animation then smoothly interpolates the frames between your key drawings. You provide these key points either as an array of values (like coordinates or colors) or as a complete CGPath for the object to trace.

How it works

You create a CAKeyframeAnimation instance for a specific property, like "position" or "backgroundColor". You then provide the animation's waypoints in one of two main properties. First, you can set the values property to an array of states, like [color1, color2, color3]. Second, for positional animations, you can set the path property to a CGPath object, and the layer's anchor point will follow that path. You can also provide an array of keyTimes (numbers from 0.0 to 1.0) to control the timing and pacing between keyframes.

When to use it

Use CAKeyframeAnimation for any animation that is not a simple A-to-B transition. It's perfect for creating a 'shake' effect on a text field by animating its position through a series of small offsets. It's also the tool for moving an object along a complex curve, like a satellite orbiting a planet, or for cycling a property through a specific sequence of colors or opacities.

When not to use it

For simple animations between two states, such as fading in or moving from left to right, CABasicAnimation is simpler and more efficient. For animations that need to feel physical, with properties like mass, stiffness, and damping, CASpringAnimation is the more appropriate choice.

One canonical example

A classic use case is making a UI element shake to indicate an error. You create a CAKeyframeAnimation for the "transform.translation.x" key path. You then set its values property to an array of horizontal offsets, like [0, 10, -10, 10, -5, 5, 0]. By setting a short duration, the layer will quickly move back and forth, creating a convincing shake effect without complex position management.

Interview question

Which animation scenario is the most appropriate use case for CAKeyframeAnimation?

  • a.Animating a view's width from 100 points to 200 points.
  • b.Guiding a layer's position along a custom, non-linear path.Correct
  • c.Applying a realistic, spring-like bounce effect to a UI element.
  • d.Smoothly transitioning a view's alpha from 1.0 to 0.0.
Why?

CAKeyframeAnimation excels at defining animations through multiple waypoints or along a complex CGPath, making it ideal for non-linear movements. Simple A-to-B transitions are better suited for CABasicAnimation, while spring-like physics are handled by CASpringAnimation.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles