Use cross fade transitions if reduce motion/prefer cross fade is enabled

This commit is contained in:
Shadowfacts 2020-09-12 13:02:19 -04:00
parent 7ac34efeab
commit ab77ad89b4
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 71 additions and 2 deletions

View File

@ -11,7 +11,11 @@ import UIKit
class SheetContainerDismissAnimationController: NSObject, UIViewControllerAnimatedTransitioning { class SheetContainerDismissAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5 if UIAccessibility.prefersCrossFadeTransitionsBackwardsCompat && (transitionContext?.viewController(forKey: .from) as? SheetContainerViewController)?.dismissAnimationInitialVelocity == nil {
return 0.25
} else {
return 0.5
}
} }
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
@ -19,6 +23,11 @@ class SheetContainerDismissAnimationController: NSObject, UIViewControllerAnimat
fatalError() fatalError()
} }
if UIAccessibility.prefersCrossFadeTransitionsBackwardsCompat && fromVC.dismissAnimationInitialVelocity == nil {
animateCrossFadeTransition(using: transitionContext)
return
}
fromVC.view.alpha = 1.0 fromVC.view.alpha = 1.0
fromVC.dimmingView.isHidden = true fromVC.dimmingView.isHidden = true
@ -57,4 +66,19 @@ class SheetContainerDismissAnimationController: NSObject, UIViewControllerAnimat
animator.startAnimation() animator.startAnimation()
} }
func animateCrossFadeTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromVC = transitionContext.viewController(forKey: .from) as? SheetContainerViewController else {
fatalError()
}
transitionContext.containerView.addSubview(fromVC.view)
let duration = transitionDuration(using: transitionContext)
UIView.animate(withDuration: duration) {
fromVC.view.alpha = 0
} completion: { (finished) in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
} }

View File

@ -11,7 +11,11 @@ import UIKit
class SheetContainerPresentationAnimationController: NSObject, UIViewControllerAnimatedTransitioning { class SheetContainerPresentationAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5 if UIAccessibility.prefersCrossFadeTransitionsBackwardsCompat {
return 0.25
} else {
return 0.5
}
} }
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
@ -19,6 +23,11 @@ class SheetContainerPresentationAnimationController: NSObject, UIViewControllerA
fatalError() fatalError()
} }
if UIAccessibility.prefersCrossFadeTransitionsBackwardsCompat {
animateCrossFadeTransition(using: transitionContext)
return
}
toVC.dimmingView.isHidden = true toVC.dimmingView.isHidden = true
let finalFrame = transitionContext.finalFrame(for: toVC) let finalFrame = transitionContext.finalFrame(for: toVC)
@ -56,4 +65,22 @@ class SheetContainerPresentationAnimationController: NSObject, UIViewControllerA
} }
func animateCrossFadeTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let toVC = transitionContext.viewController(forKey: .to) as? SheetContainerViewController else {
fatalError()
}
transitionContext.containerView.addSubview(toVC.view)
toVC.view.alpha = 0
let duration = transitionDuration(using: transitionContext)
UIView.animate(withDuration: duration) {
toVC.view.alpha = 1
} completion: { (finished) in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
} }

View File

@ -0,0 +1,18 @@
//
// File.swift
//
//
// Created by Shadowfacts on 9/12/20.
//
import UIKit
extension UIAccessibility {
static var prefersCrossFadeTransitionsBackwardsCompat: Bool {
if #available(iOS 14.0, *) {
return prefersCrossFadeTransitions
} else {
return isReduceMotionEnabled
}
}
}