Animation

The way a view changes over time to create a smooth visual transition from one state to another.

StructureiOS 13.0+macOS 10.15+tvOS 13.0+watchOS 6.0+Animation in SwiftUI

Declaration

React
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;
Swift
@frozen struct Animation

Pass a literal, or build one with SwiftUI.Animation from import { SwiftUI } from 'swiftui-react-native'.

Type properties

  • 'bouncy'SwiftUI.Animation.bouncy

    A spring animation with a predefined duration and higher amount of bounce.

  • 'default'SwiftUI.Animation.default

    A default animation instance.

  • 'easeIn'SwiftUI.Animation.easeIn

    An animation that starts slowly and then increases speed towards the end of the movement.

  • 'easeInOut'SwiftUI.Animation.easeInOut

    An animation that combines the behaviors of in and out easing animations.

  • 'easeOut'SwiftUI.Animation.easeOut

    An animation that starts quickly and then slows towards the end of the movement.

  • 'interactiveSpring'SwiftUI.Animation.interactiveSpring

    A convenience for a spring animation with a lower duration value, intended for driving interactive animations.

  • 'interpolatingSpring'SwiftUI.Animation.interpolatingSpring

    An 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.linear

    An animation that moves at a constant speed.

  • 'smooth'SwiftUI.Animation.smooth

    A smooth spring animation with a predefined duration and no bounce.

  • 'snappy'SwiftUI.Animation.snappy

    A spring animation with a predefined duration and small amount of bounce that feels more snappy.

  • 'spring'SwiftUI.Animation.spring

    A persistent spring animation. When mixed with other spring() or interactiveSpring() 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:) or repeatForever(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

See also

More in Animation.