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 ...
随机推荐
- Dev gridControl 按回车增加一行
将NewItemRowPosition属性设置为Top或Bottom, 在这样的新行中输入数据后,会自动添加到绑定的数据源中的, 如果你希望在按回车时焦点跳至下一列, 只需要设置GridView的Op ...
- XTUOJ1250 Super Fast Fourier Transform 暴力
分析:因为加起来不超过1e6,所以最多有1000+个不同的数 做法:离散化搞就好了 #include <cstdio> #include <iostream> #include ...
- asp.net 常用的几种调用存储过程的方法
(1)简单的无参数存储过程 create procedure ExpOneasselect top 10 * from Corpgo C#调用此存储过程 SqlConnection co ...
- C++实现网格水印之调试笔记(四)—— 完成嵌入
接下来的问题是,当模型是对称的时候,结果是符合预期的,但是当模型是不对称的时候,结果是错误的,如下: 输入: 顶点:233 输出: 这又是什么鬼...,我的马呢!!! 看来逻辑上还是有错误 注意这时候 ...
- 【成都GamEver游戏公司诚邀服务器伙伴】【7~15k一年4次项目奖金】
关于我们 我们厌倦了朝九晚五,一眼看到头的人生我们厌倦了耗费自己青春做的都是没有感情的项目平均从业经验5年以上行业顶尖美术和金牌制作人,资深欧美制作经验立志做中国的suppercell,公司小而美 我 ...
- 题目1069:查找学生信息(STL的map简单应用)
题目描述: 输入N个学生的信息,然后进行查询. 输入: 输入的第一行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息格式如 ...
- C++中的重载,隐藏,覆盖,虚函数,多态浅析
直到今日,才发现自己对重载的认识长时间以来都是错误的.幸亏现在得以纠正,真的是恐怖万分,雷人至极.一直以来,我认为重载可以发生在基类和派生类之间,例如: class A { public: void ...
- MEAN stack 做网站【1】
做一个小project,学习如何用MEAN技术栈来搭建网站. JavaScript新手,不足之处,请指出.(系统为win10) 搭建环境: 安装Node.JS (略过) 安装MySQL,MongoDB ...
- World’s Smallest h.264 Encoder
转载 http://www.cardinalpeak.com/blog/worlds-smallest-h-264-encoder/ View from the Peak World’s Smalle ...
- 【WPF】【火车站点信息查询】
全文涉及到的是C#和XAML 如果这两门语言并非你喜欢的语言,那可以关闭本网页了 本文介绍的是什么? 一个火车站点信息查询软件 本文涉及到的WPF基本知识 Task async await WebCl ...