1.判斷設備傾斜角度
coreMotionMgr = CMMotionManager()
coreMotionMgr!.deviceMotionUpdateInterval = 0.01
let queue = NSOperationQueue()
coreMotionMgr!.startDeviceMotionUpdatesUsingReferenceFrame(.XArbitraryZVertical, toQueue: queue) { (motion, nil) in
NSOperationQueue.mainQueue().addOperationWithBlock({
let angle = atan2(motion!.gravity.y, motion!.gravity.x)
let angleDegrees = angle * 180.0 / M_PI
print("\(Int(angleDegrees)) 度") // - 135 向左傾斜45度 -45 向右傾斜45度 向右放平是0度
})
}
2.監(jiān)聽設備旋轉
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleDeviceOrientationDidChange), name: UIDeviceOrientationDidChangeNotification, object: nil)
}
// MARK: 監(jiān)聽設備旋轉
func handleDeviceOrientationDidChange() {
let device = UIDevice.currentDevice()
switch device.orientation {
case .LandscapeLeft:
case .LandscapeRight:
case .Portrait:
case .FaceDown:
case .FaceUp: //...Unknown PortraitUpsideDown
default: break
}
}
3.強制橫屏
override func shouldAutorotate() -> Bool {
return false
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeRight
}
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。