[英]Fix Orientation and list for changes - SWIFT
我是 iOS 開發的新手,正在努力進行方向檢查。 我已經為我的視圖固定了方向,現在想查看它的方向。
我是如何修復它的:
應用代表
return self.orientationLock
}
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo
rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
查看 CONTROLLER
override func viewWillAppear(_ animated: Bool) {
AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait,
andRotateTo: UIInterfaceOrientation.portrait)
}
現在我想聽聽方向並暫時打印更改。
查看 CONTROLLER
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
switch UIDevice.current.orientation {
case .portrait:
print("portrait")
case .faceUp:
print("faceUp")
case .landscapeLeft:
print("landscape left")
case .landscapeRight:
print("landscape right")
case .portraitUpsideDown:
print("upsidedown portrait")
default:
print("something else")
}
}
麻煩的是上面的代碼是在等待視圖改變,視圖已經被鎖定,所以它永遠不會改變。
您 go 如何鎖定視圖,然后收聽手機的握持方式?
非常感激
肖恩,謝謝你的建議。 最后,這就是我所做的。 這是我的代碼
您還需要導入 CoreMotion
var motion = CMMotionManager()
var currentOrientation: String = "Portrait"
func oritentationUpdateStart() {
motion.accelerometerUpdateInterval = 1.0
motion.startAccelerometerUpdates(to: OperationQueue.current! ) { (data, error) in
if let accel = data {
if (accel.acceleration.x > 0.5){
self.currentOrientation = "Upsidedown Landscape"
}
else if (accel.acceleration.y > 0.5) {
self.currentOrientation = "Upsidedown Portrait"
}
else if (accel.acceleration.x < -0.5) {
self.currentOrientation = "Landscape"
}
else if (accel.acceleration.y < -0.5) {
self.currentOrientation = "Portrait"
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.