加速计和陀螺仪

     //返回按钮事件
     @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篇&加速计和陀螺仪的更多相关文章

  1. ios开发——实用技术篇Swift篇&播放MP3

    播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ...

  2. ios开发——实用技术篇Swift篇&地址薄、短信、邮件

    //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...

  3. ios开发——实用技术篇Swift篇&拍照

    拍照 // MARK: - 拍照 func fromPhotograph() { if UIImagePickerController.isSourceTypeAvailable(.Camera) { ...

  4. ios开发——实用技术篇Swift篇&照片选择

    照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ...

  5. ios开发——实用技术篇Swift篇&系统声音

    系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ...

  6. ios开发——实用技术篇Swift篇&视频

    视频 // MARK: - 播放视频 /*----- 播放视频 ------*/ func moviePlayerPreloadFinish(notification:NSNotification) ...

  7. ios开发——实用技术篇Swift篇&录音

    录音 // MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? / ...

  8. ios开发——实用技术篇Swift篇&多点触摸与手势识别

    多点触摸与手势识别 //点击事件 var atap = UITapGestureRecognizer(target: self, action: "tapDo:") self.vi ...

  9. ios开发——实用技术篇OC篇&iOS的主要框架

    iOS的主要框架         阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core ...

随机推荐

  1. java web 学习十(HttpServletRequest对象1)

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  2. kettle实现文本文件数据抽取方法

    KETTLE做调度的思路是,把一个有特定格式的的文本文件,写入ORACLE数据库表, 具体方法见如下操作: 首先来看下文本文件的内容: 1|test1 2|test2 3|test3 通过|进行分割的 ...

  3. HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)

    分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...

  4. Win7下硬盘安装Centos5.3

    前提声明:一个硬盘最多只能有四个主分区,也就是hda1,hda2,hda3和hda4,逻辑分区都是从hda5开始 一.软件准备:EasyBCD+分区助手+Centos 5.3 ISO镜像文件: 二.W ...

  5. c++与java的优缺点

      大多数程序员都认为C/C++会比Java语言快,甚至于觉得从Java语言诞生以来,"执行速度缓慢"的帽子就应当被扣在头顶,这种观点的出现是由于Java刚出现的时候JIT编译技术 ...

  6. unicode ansi utf-8 unicode_big_endian编码的区别

      随便说说字符集和编码  快下班时,爱问问题的小朋友Nico又问了一个问题:  "sqlserver里面有char和nchar,那个n据说是指unicode的数据,这个是什么意思.&quo ...

  7. Activity生命周期与状态保存

    弹出系统对话框,程序仍部分可见 onPause 对话框消失时 onResume   调用一个新的Activity,老的Activity不可见时 onPause->onStop 从新的Activi ...

  8. matplotlib绘制三维图

    本文参考官方文档:http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html 起步 新建一个matplotlib.figure.Figure对象, ...

  9. Android JNI之JAVA与C++对象建立对称关联(JNI优化设计,确保JNI调用的稳定性)

    转载请声明:原文转自:http://www.cnblogs.com/xiezie/p/5930503.html Android JNI之JAVA与C++对象建立对称关联 1.JAVA对象持有C++对象 ...

  10. C#应用Newtonsoft.Json操作json

    Newtonsoft.Json是一个开源的C#操作json的项目,应用起来非常简单.其github地址; 下面的代码演示了如何应用Newtonsoft.Json序列号和反序列化. using Newt ...