Skip to content

Instantly share code, notes, and snippets.

@douglashill
Created October 21, 2018 16:10
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglashill/d7e73a1240c1a3c3f816500ba296e4ed to your computer and use it in GitHub Desktop.
Save douglashill/d7e73a1240c1a3c3f816500ba296e4ed to your computer and use it in GitHub Desktop.
A UIScrollView subclass that honours the reduce motion accessibility setting
// 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