Skip to content

Device orientation observer which works even for devices with orientation-lock turned on

License

Notifications You must be signed in to change notification settings

Incetro/orientation-observer

Repository files navigation

Pod Version License

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.

Usage

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
}

Requirements

  • iOS 12.1+
  • Xcode 12.5
  • Swift 5.0

Communication

  • 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.

Installation

CocoaPods

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

Author

incetro, [email protected]

License

OrientationObserver is licensed under the MIT license. Check the LICENSE file for details.

About

Device orientation observer which works even for devices with orientation-lock turned on

Resources

License

Stars

Watchers

Forks

Packages

No packages published