ios开发——实用技术篇Swift篇&加速计和陀螺仪
加速计和陀螺仪
1 //返回按钮事件 2 @IBAction func backButtonClick() 3 { 4 self.navigationController?.popViewControllerAnimated(true) 5 } 6 7 8 @IBOutlet var xLabel:UILabel! 9 @IBOutlet var yLabel:UILabel! 10 @IBOutlet var zLabel:UILabel! 11 12 @IBOutlet var orientationLabel:UILabel! 13 14 //加速计管理者-所有的操作都会由这个motionManager接管 15 var motionManager:CMMotionManager! 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 titleLabel.text = titleString 21 22 23 24 //------ CoreMotion 加速计 25 motionManager = CMMotionManager()//注意CMMotionManager不是单例 26 motionManager.accelerometerUpdateInterval = 0.1//设置读取时间间隔 27 28 if motionManager.accelerometerAvailable//判断是否可以使用加速度计 29 { 30 //获取主线程并发队列,在主线程里跟新UI 31 motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (var accelerometerData:CMAccelerometerData?, var error:NSError?) -> Void in 32 33 if error != nil 34 { 35 self.motionManager.stopAccelerometerUpdates()//停止使用加速度计 36 }else 37 { 38 39 self.xLabel.text = "x:\(accelerometerData!.acceleration.x)" 40 self.yLabel.text = "Y:\(accelerometerData!.acceleration.y)" 41 self.zLabel.text = "Z:\(accelerometerData!.acceleration.z)" 42 } 43 }) 44 45 46 }else 47 { 48 let aler = UIAlertView(title: "您的设备不支持加速计", message: nil, delegate: nil, cancelButtonTitle: "OK") 49 aler.show() 50 } 51 52 53 54 //感知设备方向-开启监听设备方向 55 UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications() 56 57 //添加通知,监听设备方向改变 58 NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivedRotation", name: UIDeviceOrientationDidChangeNotification, object: nil) 59 60 //关闭监听设备方向 61 UIDevice.currentDevice().endGeneratingDeviceOrientationNotifications() 62 } 63 64 override func didReceiveMemoryWarning() { 65 super.didReceiveMemoryWarning() 66 // Dispose of any resources that can be recreated. 67 } 68 69 70 // MARK: - 判断设备方向代理方法 71 func receivedRotation() 72 { 73 var device = UIDevice.currentDevice() 74 75 if device.orientation == UIDeviceOrientation.Unknown 76 { 77 orientationLabel.text = "Unknown" 78 } 79 else if device.orientation == UIDeviceOrientation.Portrait 80 { 81 orientationLabel.text = "Portrait" 82 } 83 else if device.orientation == UIDeviceOrientation.PortraitUpsideDown 84 { 85 orientationLabel.text = "PortraitUpsideDown" 86 } 87 else if device.orientation == UIDeviceOrientation.LandscapeLeft 88 { 89 orientationLabel.text = "LandscapeLeft" 90 } 91 else if device.orientation == UIDeviceOrientation.LandscapeRight 92 { 93 orientationLabel.text = "LandscapeRight" 94 }else if device.orientation == UIDeviceOrientation.FaceUp 95 { 96 orientationLabel.text = "FaceUp" 97 } 98 else if device.orientation == UIDeviceOrientation.FaceDown 99 { 100 orientationLabel.text = "FaceDown" 101 } 102 } 103 104 // MARK: - 摇晃事件 105 override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) { 106 107 println("motionBegan")//开始摇晃 108 } 109 110 override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { 111 println("motionEnded")//摇晃结束 112 } 113 114 115 override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent) { 116 println("motionCancelled")//摇晃被意外终止 117 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。