How to disable transition animation when reduce motion is turned off

Amy

There's a view in my app called ContinueView that slides out of sight when a button is clicked and it works like so:

struct ContentView: View {
    @Environment(\.accessibilityReduceMotion) var isReducedMotion
    @State private var hasRequestedNotifications    = UserDefaults.standard.bool(forKey: "hasRequestedNotifications")
    @State private var isShowingContinueView        = !UserDefaults.standard.bool(forKey: "isLoggedIn")
    
    var body: some View {
     .........

        if (isShowingContinueView) {
            ContinueView(isShowingContinueView: $isShowingContinueView).transition(.asymmetric(insertion: AnyTransition.move(edge: .trailing).combined(with: .opacity), removal:  AnyTransition.move(edge: .leading).combined(with: .opacity)))
        } else if (!hasRequestedNotifications) {
            AllowNotificationsView(hasRequestedNotifications: $hasRequestedNotifications).transition(.asymmetric(insertion: AnyTransition.move(edge: .trailing).combined(with: .opacity), removal:  AnyTransition.move(edge: .leading).combined(with: .opacity)))
        }
     ........
    }
}

ContinueView:

              ....
                Button("Continue") {
                    withAnimation {
                        self.isShowingContinueView.wrappedValue = false
                    }
                }
            .....

This works perfectly, it shows and hides the view with a smooth sliding transition. But for users with reduced motion, I wanted to support it too so that instead of sliding, it just shows and hides with no animation at all.

I've got an enviroment variable at the top of my contentView called: isReducedMotion, with this I want to toggle motion on and off dependent on what the variable is set to.

I've tried putting it like this:

ContinueView(isShowingContinueView: $isShowingContinueView).transition(isReducedMotion ? nil : .asymmetric(insertion: AnyTransition.move(edge: .trailing).combined(with: .opacity), removal:  AnyTransition.move(edge: .leading).combined(with: .opacity)))

But it shows this error: 'nil' cannot be used in context expecting type 'AnyTransition'

So i'm a bit stuck with it and not sure how to get it to work. Does anyone know how to do this? Any help would be really appreciated.

P.S: The code i've put here is modified so its a bit simpler, but yes, the state variables work fine, and the isShowingContinueView variable does get set to true/false elsewhere in the code :)

Also if you have any questions feel free to hit me up since I know my question might be a bit confusing :D

jrturton

You can try using .identity instead of nil for the transition, or you can pass nil to withAnimation to specify no animation:

Button("Continue") {
    withAnimation(isReducedMotion ? nil : .default) {
        self.isShowingContinueView.wrappedValue = false
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How Moto Assist app identifies motion when Location sharing, WiFi and Mobile internet turned off?

Disable transition animation

Disable the animation/transition on the mobile

how to disable the animation of dimming the screen when ubuntu turns inactive screen off

How exactly does ROM work when the power is turned off?

How to detect onDisconnected event in firebase when wifi is suddenly turned off?

How to get sensor data in service when screen is turned off?

How to disable hover effect for the transition or animation time then enable it after the transition or animation is finished in css?

How to disable Apache and MySQL autostart when PC is turned on?

How to disable page transition animation in Ionic 4 / Angular?

How to apply curved motion to a transition?

Disable Transition Animation Between Activities

Disable a transition while an animation is running

how is disable screen off when ipa is running

Wireless problem when turned off on Windows 7

Clock is standing still when the computer is turned off

Start activity even when screen is turned off

Notifications when iPhone is turned off or app is uninstalled

chrome resolution changes when monitor is turned off

Turn vim paste mode off when it is turned on

GDCWebserver serverURL is null when wifi is turned off

Audio autoplaying even when it's turned off

How to disable an animation when opening or refereshing the page

Transition when doesn't work in Framer Motion

How do I prevent Enigmail from encrypting my email, even when encryption is turned off?

I have a follow up on "How exactly does ROM work when the power is turned off?"

How to keep graphical session running in 17.10 when external screen is turned off?

How do I draw a QFont anti-aliased when the OS has anti-alias turned off?

How to get an alert when my input volume is turned on and turn it off if I so desire?