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 ...
随机推荐
- java web 学习一
一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源( ...
- ylbtech-Model-Account(通用账户模块设计)
ylbtech-DatabaseDesgin:ylbtech-Model-Account(通用账户模块设计) ylbtech-Model-Account(通用账户模块设计) 1.A,数据库关系图(Da ...
- 《Python 学习手册4th》 第四章 介绍Python对象类型
''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容(每天看42页内容,可以保证月底看完此书) ''' ...
- JavaScript相关图书推荐
JavaScript语言精粹(修订版) 作 者 Douglas Crockford(道格拉斯·克罗克福德) 著:赵泽欣 等 译 出 版 社 电子工业出版社 出版时间 2012-09-01 版 ...
- WeChat Official Account Admin Platform Message API Guide
Keyword: WeChat Message API Text Image Location Link Event Music RichMedia Author: PondBay Studio[We ...
- 在linux上用dd命令实现ghost功能
ghost和g4l 安装操作系统,速度太慢,整个过程太冗长乏味了. 安装过程中,需要回答若干问题,系统需要安装无数个软件,创建和写入无数的文件.因为涉及到大量的文件定位和读写,速度一定是快不起来的. ...
- Java缓存学习之四:EhCache
EhCache 关键词:纯Java的进程内缓存框架.Java分布式缓存.缓存数据有两级:内存和磁盘.缓存数据会在虚拟机重启的过程中写入磁盘.是hibernate默认的缓存provider: Ehcac ...
- Cygwin的包管理器:apt-cyg
参考<Cygwin的包管理器:apt-cyg> cygwin下安装每次需要启动set_up,比较蛋疼,还是debian的apt方便,在网上看到应该cygwin 下的apt,觉得不错. 从h ...
- centos 使用mutt 命令发送邮件,随笔非教程
#按照mutt yum -y install mutt #发送邮件 echo .com -s "邮件主题" -a 附件本地地址
- 在线的JSON formate工具
一个非常好的json formate工具 可以很容易发现json的错误,以及对json进行格式化 https://jsonformatter.curiousconcept.com/