Blocks on Swift animateWithDurationanimationscompletion
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIView.animate is one of the most common APIs for simple iOS animations, and its two closure parameters do different jobs. The animations closure defines the property changes UIKit should animate. The completion closure runs afterward and tells you whether the animation finished naturally or was interrupted. Once you understand that separation, chaining and debugging animations becomes much easier.
The Two Closures Do Different Work
In modern Swift, the API is written as UIView.animate(withDuration:animations:completion:). The first closure changes view properties. UIKit captures those changes and animates from the current state to the new one.
The animations closure should contain property assignments such as alpha, center, transform, or layout changes followed by layoutIfNeeded(). The completion closure is for follow-up work such as enabling buttons, starting another animation, or removing a view.
What the finished Flag Means
The completion closure receives a Bool named finished. This is important.
- '
truemeans UIKit reached the intended end state.' - '
falsemeans the animation was interrupted or replaced before completing.'
That distinction matters if you are chaining behavior. For example, if you only want to remove a view after a fade-out truly completes, check the flag before deleting anything.
Without that check, interrupted animations can leave your UI in inconsistent states.
Animating Auto Layout Changes
For Auto Layout-based interfaces, you usually animate constraint changes rather than raw frames. The pattern is to update constraints first, then call layoutIfNeeded() inside the animation closure.
This works because UIKit interpolates the layout change over the animation duration.
Chaining Animations Cleanly
The completion closure is the usual place to start a second animation after the first one succeeds.
For simple sequences, nested closures are fine. For larger animation flows, pull each step into a helper method so the code stays readable.
Capturing self Carefully
Animation closures are usually short-lived, so retain cycles are less dangerous than with long-lived stored callbacks. Even so, if an animation is part of a larger controller lifecycle or a repeating chain, using [weak self] can still be a good habit.
The real goal is not ritual use of weak references. The goal is understanding who should keep whom alive.
Common Pitfalls
- Putting follow-up logic inside the
animationsclosure instead of thecompletionclosure. - Ignoring the
finishedflag when interruptions matter. - Changing Auto Layout constraints without calling
layoutIfNeeded()inside the animation block. - Animating properties that UIKit does not interpolate the way you expect.
- Building long chains of nested animations without extracting helper methods.
Summary
- The
animationsclosure defines what changes should animate. - The
completionclosure runs afterward and receives afinishedflag. - Use the completion block for follow-up actions and chained animations.
- For Auto Layout, change constraints first and animate
layoutIfNeeded(). - Keep animation closures focused so UI flow stays readable and predictable.
Related reading
- Bold Non-Bold Text In A Single UILabel?
- Bold Non-Bold Text In A Single UILabel?
- Bold words in a string of strings.xml in Android
- Border for an Image view in Android?
- Bordered UITextView
- BoringSSL-GRPC unsupported option '-G' for target 'arm64-apple-ios15.0
- Bots can't be created from Xcode Server hosted repository
- BottomNavigationView display both icons and text labels at all times
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.