1.判斷設(shè)備傾斜角度
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)聽設(shè)備旋轉(zhuǎn)
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleDeviceOrientationDidChange), name: UIDeviceOrientationDidChangeNotification, object: nil)
}
// MARK: 監(jiān)聽設(shè)備旋轉(zhuǎ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.強(qiáng)制橫屏
override func shouldAutorotate() -> Bool {
return false
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeRight
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者