Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quangDecember/54a86c8fd52bb41bb0f94730bb00657f to your computer and use it in GitHub Desktop.
Save quangDecember/54a86c8fd52bb41bb0f94730bb00657f to your computer and use it in GitHub Desktop.
Combine ObservableObject for SwiftUI to refresh with orientation
@available(iOS 13.0, *)
class DeviceOrientationObservable : ObservableObject {
@Published var orientation = UIDevice.current.orientation
init () {
NotificationCenter.default.addObserver(self, selector: #selector(isRotated), name: UIDevice.orientationDidChangeNotification, object: nil)
}
@objc func isRotated() {
self.orientation = UIDevice.current.orientation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment