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.监听设备旋转
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleDeviceOrientationDidChange), name: UIDeviceOrientationDidChangeNotification, object: nil)
}
// MARK: 监听设备旋转
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
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。