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 ...
随机推荐
- ylbtech-SubwayNav(地铁线路导航)-数据库设计
ylbtech-DatabaseDesgin:ylbtech-SubwayNav(地铁线路导航)-数据库设计 DatabaseName:SubwayNav(地铁线路导航) Type:线路导航 1.A, ...
- HDU 4035Maze(树状+概率dp,绝对经典)
题意: 给你n个节点的树,从1节点开始走,到每个节点都有三种情况,被杀死回到1节点,找到隐藏的出口出去,沿着当前节点相邻的边走到下一个节点,给出每个节点三种情况发生的概率分别为ki,ei,1-ki-e ...
- <转>如何测试大型ERP软件?
大型ERP软件是一个在企业范围内部应用的.高度集成的软件,且操作频繁,数据在各业务系统之间高度共享.那么针对大型ERP软件的特点,我们应该怎么测试呢?要使用怎样的测试方法?需要什么样的测试人员?... ...
- I*k->AK
将卷积转化为乘积: function A = GetA(I,m,n) %GetA get A which transforms P@k to A*k % I is the input imageP; ...
- NGUI学习笔记-Label
属性说明 Overflow: ShrinkContent : 如果文本超出文本框宽度,会自动缩小文本size,使其显示完整 ClampContent : 文本大小固定,超出文本框的部分不会显示,也不会 ...
- sensor BMA250源代码执行分析
重力传感器是根据压电效应的原理来工作的. 所谓的压电效应就是 “对于不存在对称中心的异极晶体加在晶体上的外力除了使晶体发生形变以外,还将改变晶体的极化状态,在晶体内部建立电场,这种由于机械力作用使 ...
- 七牛云存储官方接口PHP版本
PHP SDKv6 此 SDK 适用于 PHP 5.1.0 及其以上版本.基于 七牛云存储官方API 构建.使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上. ...
- bzoj 2186 [Sdoi2008]沙拉公主的困惑(欧拉函数,逆元)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2186 [题意] 若干个询问,求1..n!中与m!互质的个数. [思路] 首先有gcd( ...
- CreateThread函数&&CString::GetBuffer函数
对这个两个常见的windows下的函数学习了一下: //最简单的创建多线程实例 #include <stdio.h> #include <windows.h> //子线程函数 ...
- [Hive - Tutorial] Type System 数据类型
数据类型Type System Hive supports primitive and complex data types, as described below. See Hive Data Ty ...