NEW: Join my free 100 Days of SwiftUI challenge today! >>

How to detect the Reduce Motion accessibility setting

Paul Hudson    @twostraws   

Fully updated for Xcode 11 GM

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)) 

Here’s a complete example you can try:

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

    var body: some View {
        VStack {
            Spacer()

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

            Spacer()

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

That creates a small rectangle, 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.

LEARN SWIFTUI FOR FREE I have a massive, free SwiftUI video collection on YouTube teaching you how to build complete apps with SwiftUI – check it out!

Similar solutions…

MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5