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 ...
随机推荐
- [Everyday Mathematics]20150211 Carlson inequality
$$\bex a_n\geq 0\ra \vsm{n}a_n\leq \sqrt{\pi}\sex{\vsm{n}a_n^2}^{1/4} \sex{\vsm{n}n^2a_n^2}^{1/4}, \ ...
- FILEtoJPG-神秘文件 -更新(软件BUG及建议可以在这里反馈)
FILEtoJPG-神秘文件(文件神隐助手) 论坛神器!彩虹系列作品之神秘文件(文件神隐助手),帮你隐藏文件的好帮手! 已更新,移除对winRAR的依赖 放张大图镇帖: 此图略丑,但是很有用,文末告诉 ...
- [Hive - LanguageManual] Import/Export
LanguageManual ImportExport Skip to end of metadata Added by Carl Steinbach, last edited by Le ...
- Todolist
UValive 6041(KD tree) UValive 6042(DP) UValive 6044(图论)
- RHEL6.4 KVM 桥接上网的设置
关闭网络管理器 chkconfig NetworkManager off ##和桥接有冲突,要关闭 service NetworkManager stop 修改eth0为物理网口,br0为桥接网 ...
- C++11 能好怎?
0. 摘要 近期读了一些关于C++11标准的材料. 本篇博客将从新标准的优点.与旧版本的区别和使用方法三个角度,大致介绍我对C++11的认识. C++11标准,原名C++0x, 是03版旧标准的更新. ...
- 软件工程个人项目--Word frequency program
(一)工程设计时间预计 1.代码编写:2小时 (1)文件夹的遍历以及筛选: (2)文件夹的读取,以及对读取字符的操作: (3)所得结果排序,以及文件输出. 2.程序调试:1小时 (1)编写数据. (2 ...
- homework-01 最大子串和
题目描述 对于一个给定的数列,求该数列最大的子串和(连续) 问题分析 处理发生区间上的问题时,经常会用一个非常简单经典的思路——部分和(也有叫前缀和).部分和的思想在很多复杂的区间上的算法中都有应用, ...
- Java邮件服务学习之五:邮箱服务服务端 Apache
Apache James(Java Apache Mail Enterprise Server)是Apache组织的子项目之一,完全采用纯Java技术开发,实现了SMTP.POP3与NNTP等多种邮件 ...
- FrameWork 建模时查询项的usage
§ Identifier:代表被用于分组或汇总与其相关的Fact数据的列.也代表一个索引列.还代表日期或时间列.§ Fact:代表一个包含数值数据可被分组或汇总的列,例如,产品成本.§ Attribu ...