CABasicAnimation: Simple Property Changes Over Time

CABasicAnimation animates a single layer property from a start to an end value. Use it for fading a view (opacity), moving it (position), or scaling it (transform).
Why it exists
To provide a simple, declarative way to animate a single property of a CALayer between two states (a "from" value and a "to" value) over a specified duration. It abstracts away the complexity of manually updating property values on each frame for a smooth transition.
The mental model
Think of CABasicAnimation as setting just two keyframes: a start point and an end point for a single property. Core Animation then handles all the interpolation between them. It's like telling an actor "start at position X, end at position Y, and take 2 seconds to get there." The system figures out all the intermediate steps to make the movement look smooth.
How it works
You create an instance of CABasicAnimation, specifying the keyPath of the property you want to animate, such as "opacity", "position", or "transform.scale". You then set its toValue and, optionally, a fromValue. If fromValue is nil, the animation implicitly starts from the layer's current value. After configuring properties like duration, you add the animation object to a CALayer using its add(_:forKey:) method. The system then takes over rendering the frames.
When to use it
Use CABasicAnimation for any simple, "A-to-B" style animation on a single property. It's perfect for fading elements in or out (opacity), moving them across the screen (position), rotating them (transform.rotation), or resizing them (bounds or transform.scale). It is the simplest tool for the most common animation tasks.
When not to use it
Avoid it for complex paths or multi-stage animations. If you need to animate a property through several specific values (A -> B -> C), use CAKeyframeAnimation instead. For animations that need to feel physical, with bounce or springiness, CASpringAnimation is the correct choice. For animating multiple properties together in a synchronized group, use CAAnimationGroup.
One canonical example
To fade out a UIView, you animate its backing CALayer's opacity. First, create a CABasicAnimation with the key path "opacity" and set its toValue to 0.0. Set a duration, like 0.5 seconds. Then, add this animation to the layer. The critical footgun to avoid is the "snap back": the animation only affects the temporary presentation state. To make the change permanent, you must also update the model value itself: myView.layer.opacity = 0.0. Without this line, the view would reappear instantly when the animation finishes.
Interview question
For which animation task would CABasicAnimation be the LEAST appropriate choice?
- a.Making a UI element smoothly appear by animating its opacity from 0 to 1.
- b.Resizing an image view from a small thumbnail to a larger preview.
- c.Gradually changing a view's background color from red to blue.
- d.Animating an object to follow a predefined curved path across the screen.Correct
Why? this is the answer
CABasicAnimation is designed for simple "A-to-B" animations of a single property. Animating an object along a complex path requires CAKeyframeAnimation, as explicitly stated in the card under "When NOT to use it".
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.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles