Animation
The way a view changes over time to create a smooth visual transition from one state to another.
Declaration
type Animation =
| 'bouncy'
| 'default'
| 'easeIn'
| 'easeInOut'
| 'easeOut'
| 'interactiveSpring'
| 'interpolatingSpring'
| 'linear'
| 'smooth'
| 'snappy'
| 'spring'
| { bouncy: { duration?: number; extraBounce?: number; }; }
| { easeIn: { duration: number; }; }
| { easeInOut: { duration: number; }; }
| { easeOut: { duration: number; }; }
| { interactiveSpring: { duration?: number; extraBounce?: number; blendDuration?: number; }; }
| { interactiveSpring: { response?: number; dampingFraction?: number; blendDuration?: number; }; }
| { interpolatingSpring: { duration?: number; bounce?: number; initialVelocity?: number; }; }
| { interpolatingSpring: { mass?: number; stiffness: number; damping: number; initialVelocity?: number; }; }
| { interpolatingSpring: { spring: Spring; initialVelocity?: number; }; }
| { linear: { duration: number; }; }
| { smooth: { duration?: number; extraBounce?: number; }; }
| { snappy: { duration?: number; extraBounce?: number; }; }
| { spring: { duration?: number; bounce?: number; blendDuration?: number; }; }
| { spring: { response?: number; dampingFraction?: number; blendDuration?: number; }; }
| { spring: { spring: Spring; blendDuration?: number; }; }
| { timingCurve: { curve: UnitCurve; duration: number; }; }
| { timingCurve: { p1x: number; p1y: number; p2x: number; p2y: number; duration?: number; }; }
| AnimationChain;@frozen struct AnimationPass a literal, or build one with SwiftUI.Animation from import { SwiftUI } from 'swiftui-react-native'.
Type properties
'bouncy'SwiftUI.Animation.bouncyA spring animation with a predefined duration and higher amount of bounce.
'default'SwiftUI.Animation.defaultA default animation instance.
'easeIn'SwiftUI.Animation.easeInAn animation that starts slowly and then increases speed towards the end of the movement.
'easeInOut'SwiftUI.Animation.easeInOutAn animation that combines the behaviors of in and out easing animations.
'easeOut'SwiftUI.Animation.easeOutAn animation that starts quickly and then slows towards the end of the movement.
'interactiveSpring'SwiftUI.Animation.interactiveSpringA convenience for a
springanimation with a lowerdurationvalue, intended for driving interactive animations.'interpolatingSpring'SwiftUI.Animation.interpolatingSpringAn interpolating spring animation that uses a damped spring model to produce values in the range [0, 1] that are then used to interpolate within the [from, to] range of the animated property. Preserves velocity across overlapping animations by adding the effects of each animation.
'linear'SwiftUI.Animation.linearAn animation that moves at a constant speed.
'smooth'SwiftUI.Animation.smoothA smooth spring animation with a predefined duration and no bounce.
'snappy'SwiftUI.Animation.snappyA spring animation with a predefined duration and small amount of bounce that feels more snappy.
'spring'SwiftUI.Animation.springA persistent spring animation. When mixed with other
spring()orinteractiveSpring()animations on the same property, each animation will be replaced by their successor, preserving velocity from one animation to the next. Optionally blends the response values between springs over a time period.
Type methods
Overview
An Animation provides a visual transition of a view when a state value
changes from one value to another. The characteristics of this transition
vary according to the animation type. For instance, a linear animation
provides a mechanical feel to the animation because its speed is consistent
from start to finish. In contrast, an animation that uses easing, like
easeOut, offers a more natural feel by varying the acceleration
of the animation.
To apply an animation to a view, add the View.animation(_:value:)
modifier, and specify both an animation type and the value to animate. For
instance, the Circle view in the following code performs an
easeIn animation each time the state variable scale changes:
struct ContentView: View {
@State private var scale = 0.5
var body: some View {
VStack {
Circle()
.scaleEffect(scale)
.animation(.easeIn, value: scale)
HStack {
Button("+") { scale += 0.1 }
Button("-") { scale -= 0.1 }
}
}
.padding()
}@Video(source: "animation-01-overview-easein.mp4", poster: "animation-01-overview-easein.png", alt: "A video that shows a circle enlarging then shrinking to its original size using an ease-in animation.")
When the value of scale changes, the modifier
View.scaleEffect(_:anchor:) resizes Circle according to the
new value. SwiftUI can animate the transition between sizes because
Circle conforms to the Shape protocol. Shapes in SwiftUI conform to
the Animatable protocol, which describes how to animate a property of a
view.
In addition to adding an animation to a view, you can also configure the animation by applying animation modifiers to the animation type. For example, you can:
- Delay the start of the animation by using the
delay(_:)modifier. - Repeat the animation by using the
repeatCount(_:autoreverses:)orrepeatForever(autoreverses:)modifiers. - Change the speed of the animation by using the
speed(_:)modifier.
For example, the Circle view in the following code repeats
the easeIn animation three times by using the
repeatCount(_:autoreverses:) modifier:
struct ContentView: View {
@State private var scale = 0.5
var body: some View {
VStack {
Circle()
.scaleEffect(scale)
.animation(.easeIn.repeatCount(3), value: scale)
HStack {
Button("+") { scale += 0.1 }
Button("-") { scale -= 0.1 }
}
}
.padding()
}
}@Video(source: "animation-02-overview-easein-repeat.mp4", poster: "animation-02-overview-easein-repeat.png", alt: "A video that shows a circle that repeats the ease-in animation three times: enlarging, then shrinking, then enlarging again. The animation reverses causing the circle to shrink, then enlarge, then shrink to its original size.")
A view can also perform an animation when a binding value changes. To
specify the animation type on a binding, call its Binding.animation(_:)
method. For example, the view in the following code performs a
linear animation, moving the box truck between the leading and trailing
edges of the view. The truck moves each time a person clicks the Toggle
control, which changes the value of the $isTrailing binding.
struct ContentView: View {
@State private var isTrailing = false
var body: some View {
VStack(alignment: isTrailing ? .trailing : .leading) {
Image(systemName: "box.truck")
.font(.system(size: 64))
Toggle("Move to trailing edge",
isOn: $isTrailing.animation(.linear))
}
}
}@Video(source: "animation-03-overview-binding.mp4", poster: "animation-03-overview-binding.png", alt: "A video that shows a box truck that moves from the leading edge of a view to the trailing edge. The box truck then returns to the view's leading edge.")
From Apple’s SwiftUI documentation. Code samples there are Swift.
Used by
- PhaseAnimatorA container that animates its content by automatically cycling through a collection of phases that you provide, each defining a discrete step within an animation.
- animationApplies the given animation to all animatable values within the body closure.
- phaseAnimatorAnimates effects that you apply to a view over a sequence of phases that change continuously.
- transactionApplies the given transaction mutation function to all animations used within the view.
- withAnimationReturns the result of recomputing the view's body with the provided animation.
See also
More in Animation.
- AnimationChainAnimation with chained calls: SwiftUI.Animation.delay(...).
- AnimationCompletionCriteriaThe criteria that determines when an animation is considered finished.
- AnimationTimelineScheduleA pausable schedule of dates updating at a frequency no more quickly than the provided interval.
- AnyTransitionA type-erased transition.
- AnyTransitionChainAnyTransition with chained calls: SwiftUI.AnyTransition.animation(...).
- BlurReplaceTransitionA transition that animates the insertion or removal of a view by combining blurring and scaling effects.
- BlurReplaceTransitionConfigurationConfiguration properties for a transition.
- ContentTransitionA kind of transition that applies to the content within a single view, rather than to the insertion or removal of a view.
- DropSessionPhaseThe phase of the current drop session.
- GlassEffectTransitionA structure that describes changes to apply when a glass effect is added or removed from the view hierarchy.
- IdentityTransitionA transition that returns the input view, unmodified, as the output view.
- KeyframesA type that defines changes to a value over time.