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 ...
随机推荐
- Oracle 数据乱码
原文 Oracle 数据乱码 服务器配置环境变量 NLS_LANG:American_america.ZHS16GBK
- 微信多媒体上传图片,创建卡券上传 LOGO
//*****************************************多媒体上传图片 begin******************************************** ...
- <转>亲手缔造DNS体系,创建DNS私有根:DNS系列之六
打造DNS私有根 我们现在已经从前面的博文中了解到了很多DNS的相关知识,今天我们用一个综合性的实验把前面的内容都串起来复习一下,这个有趣的实验就是DNS的私有根.私有根顾名思义是由个人或企业自行创建 ...
- pycharm 源文件 编码格式
pycharm 4.5.3 版本 修改项目的编码 按如下操作:菜单 File-Settings-Editor-File Encodings 将IDE Encoding 和 Project Encodi ...
- Chapter 1 初探Caffe
首先下载windows下源码: Microsoft 官方:GitHub - Microsoft/caffe: Caffe on both Linux and Windows 官方源码使用Visual ...
- LeetCode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...
- Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)
题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...
- public, protected, private, internal, protected internal简析
public是可访问权限最高的,比如姓名,每个人都可以知道别人的姓名,这个不是什么秘密 protected的访问权限要低些,只有子类才可以访问得到父类的protected属性.就好像老子的财产只有儿子 ...
- LinkButton(按钮)
使用$.fn.linkbutton.defaults重写默认值对象. 按钮组件使用超链接按钮创建.它使用一个普通的<a>标签进行展示.它可以同时显示一个图标和文本,或只有图标或文字.按钮的 ...
- C# 6.0 的新特性
1. 自动的属性初始化器Auto Property initialzier 之前的方式: public class AutoPropertyBeforeCsharp6 { private string ...