Skip to content

Instantly share code, notes, and snippets.

@emin-grbo
Last active October 6, 2022 18:22
Show Gist options
  • Save emin-grbo/05a538cb9b829739341a00c6e25eb9d4 to your computer and use it in GitHub Desktop.
Save emin-grbo/05a538cb9b829739341a00c6e25eb9d4 to your computer and use it in GitHub Desktop.
struct AnimationWithReduceMotion<V: Equatable>: ViewModifier {
@Environment(\.accessibilityReduceMotion)
private var reduceMotion
var animation: Animation?
let value: V
func body(content: Content) -> some View {
content
.animation(reduceMotion ? nil : animation, value: value)
}
}
extension View {
func animationWithReducedMotion<V>(_ animation: Animation?,
value: V) -> some View where V : Equatable {
modifier(AnimationWithReduceMotion(animation: animation, value: value))
}
}
@TerjeLon
Copy link

TerjeLon commented Oct 6, 2022

struct AnimationWithReduceMotion<V: Equatable>: ViewModifier {
  
  var animation: Animation?
  let value: V
  
  func body(content: Content) -> some View {
      return content
          .animation(UIAccessibility.isReduceMotionEnabled ? nil : animation, value: value)
  }
}

extension View {
  func animationWithReducedMotion<V>(_ animation: Animation?,
                                     value: V) -> some View where V : Equatable {
    modifier(AnimationWithReduceMotion(animation: animation, value: value))
  }
}

This works. Previously it change Equatable from some to any in the ViewModifier, but now both treat it as some.
Also when returning content and content.animation it yields 2 different views, so it could not conform to some View, that's why I changed it to return animation with nil on reducedMotion instead

Lastly, previously it animated when isReduceMotionEnabled was true, I swapped it around to only animate when its set to false

@emin-grbo
Copy link
Author

Ohhhh snap...bloody briliant!! 😅

Not sure how it completely slipped my mind to introduce it at struct signature level 🤦‍♂️

I Just made a small adjustment with @env var
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment