Created
October 21, 2018 16:10
-
-
Save douglashill/d7e73a1240c1a3c3f816500ba296e4ed to your computer and use it in GitHub Desktop.
A UIScrollView subclass that honours the reduce motion accessibility setting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Douglas Hill, October 2018 | |
import UIKit | |
/** | |
A scroll view that honours the reduce motion accessibility setting. | |
If reduce motion is enabled, animated adjustments to contentOffset | |
will use a cross dissolve instead of translation. | |
This does not handle scrolling to the top when tapping the status bar. | |
To use a cross dissolve for that, the scroll view’s delegate must | |
implement scrollViewShouldScrollToTop: by scrolling to the top manually | |
and returing false. | |
*/ | |
class ReduceMotionScrollView: UIScrollView { | |
override func setContentOffset(_ newContentOffset: CGPoint, animated: Bool) { | |
guard | |
animated, | |
UIAccessibility.isReduceMotionEnabled, | |
let superview = superview, | |
let snapshot = self.snapshotView(afterScreenUpdates: false) else | |
{ | |
super.setContentOffset(newContentOffset, animated: animated) | |
return | |
} | |
snapshot.frame = frame | |
superview.insertSubview(snapshot, aboveSubview: self) | |
super.setContentOffset(newContentOffset, animated: false) | |
UIView.transition(with: superview, duration: 0.2, options: .transitionCrossDissolve, animations: { | |
snapshot.removeFromSuperview() | |
}, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment