1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/iOS/OrientationTracker.swift

97 lines
3.1 KiB
Swift
Raw Normal View History

2022-07-10 03:59:13 +05:30
import CoreMotion
import UIKit
public class OrientationTracker {
public static let shared = OrientationTracker()
public static let deviceOrientationChangedNotification = NSNotification.Name("DeviceOrientationChangedNotification")
public var currentDeviceOrientation: UIDeviceOrientation = .portrait
public var currentInterfaceOrientation: UIInterfaceOrientation {
switch currentDeviceOrientation {
case .landscapeLeft:
return .landscapeLeft
case .landscapeRight:
return .landscapeRight
default:
return .portrait
}
}
public var currentInterfaceOrientationMask: UIInterfaceOrientationMask {
switch currentInterfaceOrientation {
case .landscapeLeft:
return .landscapeLeft
case .landscapeRight:
return .landscapeRight
default:
return .portrait
}
}
public var affineTransform: CGAffineTransform {
var angleRadians: Double
switch currentDeviceOrientation {
case .portrait:
angleRadians = 0
case .landscapeLeft:
angleRadians = -0.5 * .pi
case .landscapeRight:
angleRadians = 0.5 * .pi
case .portraitUpsideDown:
angleRadians = .pi
default:
return .identity
}
return CGAffineTransform(rotationAngle: angleRadians)
}
private let motionManager: CMMotionManager
private let queue: OperationQueue
private init() {
motionManager = CMMotionManager()
motionManager.accelerometerUpdateInterval = 0.1
queue = OperationQueue()
}
public func startDeviceOrientationTracking() {
motionManager.startAccelerometerUpdates(to: queue) { accelerometerData, error in
guard error == nil else { return }
2022-09-28 19:57:01 +05:30
guard let accelerometerData else { return }
2022-07-10 03:59:13 +05:30
let newDeviceOrientation = self.deviceOrientation(forAccelerometerData: accelerometerData)
guard newDeviceOrientation != self.currentDeviceOrientation else { return }
self.currentDeviceOrientation = newDeviceOrientation
NotificationCenter.default.post(name: Self.deviceOrientationChangedNotification,
object: nil,
userInfo: nil)
}
}
public func stopDeviceOrientationTracking() {
motionManager.stopAccelerometerUpdates()
}
private func deviceOrientation(forAccelerometerData accelerometerData: CMAccelerometerData) -> UIDeviceOrientation {
2022-08-10 00:39:49 +05:30
let threshold = 0.55
if accelerometerData.acceleration.x >= threshold {
2022-07-10 03:59:13 +05:30
return .landscapeLeft
2023-06-17 17:39:51 +05:30
}
if accelerometerData.acceleration.x <= -threshold {
2022-07-10 03:59:13 +05:30
return .landscapeRight
2023-06-17 17:39:51 +05:30
}
if accelerometerData.acceleration.y <= -threshold {
2022-07-10 03:59:13 +05:30
return .portrait
2023-06-17 17:39:51 +05:30
}
2023-11-25 23:56:44 +05:30
if UIDevice.current.userInterfaceIdiom == .pad && accelerometerData.acceleration.y >= threshold {
2022-07-10 03:59:13 +05:30
return .portraitUpsideDown
}
2023-11-25 23:56:44 +05:30
2023-06-17 17:39:51 +05:30
return currentDeviceOrientation
2022-07-10 03:59:13 +05:30
}
}