OrientationObserver is a CoreMotion-based library for tracking device orientation changes. It also works for devices with orientation-lock turned on. You can subscribe to orientation change notifications and react accordingly.
To start device orientation tracking use OrientationObserver
and startDeviceOrientationTracking
method. You should add an observer for the OrientationObserver.deviceOrientationChangedNotification
and react to orientation changes:
notificationCenter.addObserver(
self,
selector: #selector(didChangeDeviceOrientation),
name: OrientationObserver.deviceOrientationChangedNotification,
object: nil
)
You can access the current orientation using the currentDeviceOrientation
property. To stop receiving notifications use the stopDeviceOrientationTracking
method. If your app supports portrait mode only, you can use the affineTransform
property to rotate views when the orientation changes:
UIView.animate(withDuration: 0.25) { [weak self] in
self?.imageView.transform = self?.orientationObserver.affineTransform
}
- iOS 12.1+
- Xcode 12.5
- Swift 5.0
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate OrientationObserver into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
target '<Your Target Name>' do
pod 'orientation-observer'
end
Then, run the following command:
$ pod install
incetro, [email protected]
OrientationObserver is licensed under the MIT license. Check the LICENSE file for details.