Skip to main content

How to detect the Reduce Motion accessibility setting

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to detect the Reduce Motion accessibility setting 관련

SwiftUI by Example

Back to Home

How to detect the Reduce Motion accessibility setting | SwiftUI by Example

How to detect the Reduce Motion accessibility setting

Updated for Xcode 15

Many users are sensitive to animations, particularly those are large or complex. As a result, iOS has a built-in accessibility setting called Reduce Motion, which apps can read and respond to as appropriate.

In SwiftUI, this setting is exposed to us as an environment Boolean, so you should start by adding a property for it to your views:

@Environment(\.accessibilityReduceMotion) var reduceMotion

Now it’s down to you to decide what “reduce motion” means – should you remove your animations, or just change them to be less strong? Should you keep some important animations and just remove the ones that are for visual appeal?

For example, if you wanted a bouncy spring animation for most users, but no animation at all for users who want reduced motion, you might use an animation modifier like this one:

.animation(reduceMotion ? nil : .spring(response: 1, dampingFraction: 0.1), value: someValue) 

Here’s a complete example you can try:

struct ContentView: View {
    @Environment(\.accessibilityReduceMotion) var reduceMotion
    @State private var scale = 1.0

    var body: some View {
        VStack {
            Spacer()

            Circle()
                .frame(width: 20, height: 20)
                .scaleEffect(scale)
                .animation(reduceMotion ? nil : .spring(response: 1, dampingFraction: 0.1), value: scale)

            Spacer()

            Button("Increase Scale") {
                scale *= 1.5
            }
        }
    }
}

That creates a small circle, scaling it up with a spring animation every time the button is pressed. But if the user enables Reduce Motion, the animation is removed entirely – it uses nil for the animation() modifier.

Similar solutions…
How to reduce animations when requested | SwiftUI by Example

How to reduce animations when requested
How to use decorative images to reduce screen reader clutter | SwiftUI by Example

How to use decorative images to reduce screen reader clutter
Introduction to accessibility with SwiftUI | SwiftUI by Example

Introduction to accessibility with SwiftUI
How to detect shake gestures | SwiftUI by Example

How to detect shake gestures
How to detect the location of a tap inside a view | SwiftUI by Example

How to detect the location of a tap inside a view

이찬희 (MarkiiimarK)
Never Stop Learning.