https://github.com/amerhukic/orientationtracker
Track device orientation changes even for devices with orientation-lock turned on.
https://github.com/amerhukic/orientationtracker
device-orientation ios orientation-detection orientation-tracking swift
Last synced: 3 months ago
JSON representation
Track device orientation changes even for devices with orientation-lock turned on.
- Host: GitHub
- URL: https://github.com/amerhukic/orientationtracker
- Owner: amerhukic
- License: mit
- Created: 2019-02-13T21:45:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-16T16:15:00.000Z (over 5 years ago)
- Last Synced: 2025-03-18T13:02:16.393Z (about 1 year ago)
- Topics: device-orientation, ios, orientation-detection, orientation-tracking, swift
- Language: Swift
- Homepage: https://www.amerhukic.com
- Size: 205 KB
- Stars: 19
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
**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.
## Requirements
- iOS 8.0+
- Xcode 10.2
- Swift 5.0
## Usage
To start device orientation tracking use `OrientationTracker` you simply invoke the `startDeviceOrientationTracking` method, preferably in `AppDelegate` :
```swift
DeviceOrientationDetector.shared.startDeviceOrientationTracking()
```
You then add an observer for the `OrientationTracker.deviceOrientationChangedNotification` and react to orientation changes:
```swift
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:
```swift
DeviceOrientationDetector.shared.stopDeviceOrientationTracking()
```
If your app supports portrait mode only, you can use the `affineTransform` property to rotate views when the orientation changes:
```swift
UIView.animate(withDuration: 0.2) {
self.imageView.transform = DeviceOrientationDetector.shared.affineTransform
}
```
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Installation
### CocoaPods
[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. You can install it with the following command:
```bash
$ gem install cocoapods
```
To integrate OrientationTracker into your Xcode project using CocoaPods, specify it in your `Podfile`:
```ruby
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '' do
pod 'OrientationTracker'
end
```
Then, run the following command:
```bash
$ pod install
```
## Author
[Amer Hukić](https://amerhukic.com)
## License
OrientationTracker is licensed under the MIT license. Check the [LICENSE](LICENSE) file for details.