ios开发——实用技术篇Swift篇&加速计和陀螺仪
加速计和陀螺仪
     //返回按钮事件
     @IBAction func backButtonClick()
     {
         self.navigationController?.popViewControllerAnimated(true)
     }
     @IBOutlet var xLabel:UILabel!
     @IBOutlet var yLabel:UILabel!
     @IBOutlet var zLabel:UILabel!
     @IBOutlet var orientationLabel:UILabel!
     //加速计管理者-所有的操作都会由这个motionManager接管
     var motionManager:CMMotionManager!
     override func viewDidLoad() {
         super.viewDidLoad()
         titleLabel.text = titleString
         //------ CoreMotion 加速计
         motionManager = CMMotionManager()//注意CMMotionManager不是单例
         motionManager.accelerometerUpdateInterval = 0.1//设置读取时间间隔
         if motionManager.accelerometerAvailable//判断是否可以使用加速度计
         {
             //获取主线程并发队列,在主线程里跟新UI
             motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (var accelerometerData:CMAccelerometerData?, var error:NSError?) -> Void in
                 if error != nil
                 {
                     self.motionManager.stopAccelerometerUpdates()//停止使用加速度计
                 }else
                 {
                     self.xLabel.text = "x:\(accelerometerData!.acceleration.x)"
                     self.yLabel.text = "Y:\(accelerometerData!.acceleration.y)"
                     self.zLabel.text = "Z:\(accelerometerData!.acceleration.z)"
                 }
             })
         }else
         {
             let aler = UIAlertView(title: "您的设备不支持加速计", message: nil, delegate: nil, cancelButtonTitle: "OK")
             aler.show()
         }
         //感知设备方向-开启监听设备方向
         UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
         //添加通知,监听设备方向改变
         NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivedRotation", name: UIDeviceOrientationDidChangeNotification, object: nil)
         //关闭监听设备方向
         UIDevice.currentDevice().endGeneratingDeviceOrientationNotifications()
     }
     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }
     // MARK: - 判断设备方向代理方法
     func receivedRotation()
     {
         var device = UIDevice.currentDevice()
         if device.orientation == UIDeviceOrientation.Unknown
         {
             orientationLabel.text = "Unknown"
         }
         else if device.orientation == UIDeviceOrientation.Portrait
         {
             orientationLabel.text = "Portrait"
         }
         else if device.orientation == UIDeviceOrientation.PortraitUpsideDown
         {
              orientationLabel.text = "PortraitUpsideDown"
         }
         else if device.orientation == UIDeviceOrientation.LandscapeLeft
         {
              orientationLabel.text = "LandscapeLeft"
         }
         else if device.orientation == UIDeviceOrientation.LandscapeRight
         {
              orientationLabel.text = "LandscapeRight"
         }else if device.orientation == UIDeviceOrientation.FaceUp
         {
              orientationLabel.text = "FaceUp"
         }
         else  if device.orientation == UIDeviceOrientation.FaceDown
         {
              orientationLabel.text = "FaceDown"
         }
     }
     // MARK: - 摇晃事件
     override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) {
         println("motionBegan")//开始摇晃
     }
     override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
         println("motionEnded")//摇晃结束
     }
     override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent) {
         println("motionCancelled")//摇晃被意外终止
     }
ios开发——实用技术篇Swift篇&加速计和陀螺仪的更多相关文章
- ios开发——实用技术篇Swift篇&播放MP3
		播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ... 
- ios开发——实用技术篇Swift篇&地址薄、短信、邮件
		//返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ... 
- ios开发——实用技术篇Swift篇&拍照
		拍照 // MARK: - 拍照 func fromPhotograph() { if UIImagePickerController.isSourceTypeAvailable(.Camera) { ... 
- ios开发——实用技术篇Swift篇&照片选择
		照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ... 
- ios开发——实用技术篇Swift篇&系统声音
		系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ... 
- ios开发——实用技术篇Swift篇&视频
		视频 // MARK: - 播放视频 /*----- 播放视频 ------*/ func moviePlayerPreloadFinish(notification:NSNotification) ... 
- ios开发——实用技术篇Swift篇&录音
		录音 // MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? / ... 
- ios开发——实用技术篇Swift篇&多点触摸与手势识别
		多点触摸与手势识别 //点击事件 var atap = UITapGestureRecognizer(target: self, action: "tapDo:") self.vi ... 
- ios开发——实用技术篇OC篇&iOS的主要框架
		iOS的主要框架 阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core ... 
随机推荐
- DevExpress licenses.licx 的解决方法  z
			在 使用DevExpress控件的时候.每次对窗体进行更改的时候,都会出现一个对话框.发布的时候 也会出现一个对话框.之前的解决方法是在发布的时候把licenses.licx给删除掉,但是这个方法治标 ... 
- Long Dominoes(ZOJ 2563状压dp)
			题意:n*m方格用1*3的方格填充(不能重叠)求有多少种填充方法 分析:先想状态,但想来想去就是觉得不能覆盖所有情况,隔了一天,看看题解,原来要用三进制 0 表示横着放或竖放的最后一行,1表示竖放的中 ... 
- IOS color 颜色值比较
			/生成采样对照颜色(黑色) UIColor* sampleColor = [UIColor colorWithRed:(0/255.0f) green:(0/255.0f) blue:(0/255. ... 
- 如何从ST网站找到对应的固件库
			ST官方网站改版后,基本上很难搜索到固件库的地址,找了半天才找到固件库的下载地址,通过此方法可以找到其他需要的资源,故记下来方便大家. 下载的网站地址为: Home>Tools and Soft ... 
- python easy_install centos 下安装过程和原理解析
			一.easy_install 安装过程 其安装过程有很多种,我也找了很多的例子,但是结果都不太好,以下方法的结果是不错的. easy_install与yum类似,使用easy_install,可以轻松 ... 
- BSON与JSON的区别
			BSON是由10gen开发的一个数据格式,目前主要用于MongoDB中,是MongoDB的数据存储格式.BSON基于JSON格式,选择JSON进行改造的原因主要是JSON的通用性及JSON的schem ... 
- JAVA虚拟机之类加载器
			转载请声明:原文转自http://www.cnblogs.com/xiezie/p/5909570.html 1.JVM的生命周期 1.1 JVM的生命周期和程序的生命周期一致 1.2 JVM结束生命 ... 
- linux appear packet loss solution
			故障排查: 早上突然收到nagios服务器check_icmp的报警,报警显示一台网站服务器的内网网络有问题.因为那台服务器挂载了内网的NFS,因此内网的网络就采用nagios的check_icmp来 ... 
- 2048-AI程序算法分析
			转自:CodingLabs 针对目前火爆的2048游戏,有人实现了一个AI程序,可以以较大概率(高于90%)赢得游戏,并且作者在stackoverflow上简要介绍了AI的算法框架和实现思路.但是这个 ... 
- Android问题-新电脑新系统WIN764位上安装简版本的XE8提示“Unit not found: 'System'”
			问题现象:电脑太慢,重安新系统,所以要安DELPHIXE8,但安装过程中出现二次杀软件提示,我都选的是通过.但是XE8过程到最后的"Create AVD"时出现一个错误(具体是什么 ... 
