OrientationTracker is a Core Motion 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.
- iOS 8.0+
- Xcode 10.2
- Swift 5.0
To start device orientation tracking use OrientationTracker
you simply invoke the startDeviceOrientationTracking
method, preferably in AppDelegate
:
DeviceOrientationDetector.shared.startDeviceOrientationTracking()
You then add an observer for the OrientationTracker.deviceOrientationChangedNotification
and react to orientation changes:
NotificationCenter.default.addObserver(self, selector: #selector(updateViews), name: DeviceOrientationDetector.deviceOrientationChangedNotification, object: nil)
You can access the current orientation using the currentDeviceOrientation
property.
To stop receiving notifications use the stopDeviceOrientationTracking
method:
DeviceOrientationDetector.shared.stopDeviceOrientationTracking()
If your app supports portrait mode only, you can use the affineTransform
property to rotate views when the orientation changes:
UIView.animate(withDuration: 0.2) {
self.imageView.transform = DeviceOrientationDetector.shared.affineTransform
}
To run the example project, clone the repo, and run pod install
from the Example directory first.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate OrientationTracker into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '<Your Target Name>' do
pod 'OrientationTracker'
end
Then, run the following command:
$ pod install
OrientationTracker is licensed under the MIT license. Check the LICENSE file for details.