微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 加速传感器CoreMotion的用法,小球加速运动并反弹样例

1,加速传感器可以监听到x,y,z三个方向的加速度,使用步骤如下:
(1)实例化cmmotionmanager
(2)向cmmotionmanager的accelerometerUpdateInterval属性中设置通知间隔时间值。
(3)使用NSOperationQueue.currentQueue()建立一个监听队列。
(4)使用startAccelerometerUpdatesToQueue方法更新监听队列,并设置回调函数用于接受加速度通知。在回调函数中使用accelerometerData.acceleration相关属性可以获取x、y、z各个方向的加速度。

2,通知频率设置建议
accelerometerUpdateInterval表示通知频率,表示间隔多少秒通知一次。iPhone开发文档中推荐使用的通知间隔如下:
(1)检测设备朝向:1/10 ~ 1/20
(2)在游戏中需要实时使用加速传感器时:1/30 ~ 1/60
(3)检测敲击设备或者剧烈摇动设备的情况下:1/70 ~ 1/100
3,x,z轴
(1)对于iphone手机来说,画面上下为y轴,左右为z轴,贯穿屏幕为z轴。
(2)向上,向右,手机的前面分别是各轴的正方向。
4,加速度(原始加速度)
加速度不仅受震动手机时施加的作用力的影响,还会持续受到重力的影响。因此iphone手机如果垂直拿在手上的话,Y轴负方向将受重力作用,加速度y属性将一直为负值(最小值为-1.0)

5,Gravity和Useracceleration
网友cruise_H问:motionManager.deviceMotion.useracceleration.x和motionManager.accelerometerData!.acceleration.x两个获取acceleration有什么区别?
上面提到的原始的加速度(即通过startAccelerometerUpdates获取的那个值)实际上是由两种加速度合成而来的。一个是重力加速度(Gravity),一个用户对手机施加的加速度(Useracceleration)(当然我们也可以分别获取这两种加速度)。
所以当手机垂直静止时,虽然Useracceleration是0,但由于有重力加速度,所以两个合成后加速度y属性便是为负值。

6,测试样例
该应用运行后,将在画面正中央显示一个球体。倾斜手机时,球体将向着倾斜的方向运动。碰撞到四壁后反弹回来。
(注意:由于需要用到设备的重力感应器,所以要使用真机调试,模拟器运行小球不会动。)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import UIKit
CoreMotion
class ViewController : UIViewController , UIAccelerometerDelegate {
var ball: UIImageView !
speedX: UIaccelerationValue =0
speedY: =0
motionManager = cmmotionmanager ()
override func viewDidLoad() {
super .viewDidLoad()
//放一个小球在中央
ball= (image: UIImage (named: "ball" ))
ball.frame= CGRectMake (0,50,50)
ball.center= self .view.center
.view.addSubview(ball)
motionManager.accelerometerUpdateInterval = 1/60
if (motionManager.accelerometeravailable)
{
queue = NSOperationQueue .currentQueue()
motionManager.startAccelerometerUpdatesToQueue(queue,withHandler:
{(accelerometerData : CMAccelerometerData !,error : NSError !) in
//动态设置小球位置
.speedX += accelerometerData.acceleration.x
.speedY += accelerometerData.acceleration.y
posX= .ball.center.x + CGFloat ( .speedX)
posY= .ball.center.y - .speedY)
//碰到边框后的反弹处理
posX<0 {
posX=0;
//碰到左边的边框后以0.4倍的速度反弹
.speedX *= -0.4
} else posX > .view.bounds.size.width {
.view.bounds.size.width
//碰到右边的边框后以0.4倍的速度反弹
.speedX *= -0.4
}
posY<0 {
posY=0
//碰到上面的边框不反弹
.speedY=0
posY> .view.bounds.size.height{
.view.bounds.size.height
//碰到下面的边框以1.5倍的速度反弹
.speedY *= -1.5
}
.ball.center= CGPointMake (posX,posY)
})
}
}
}

(上面是使用Xcode6.4编写的,在Xcode7 beta4中由于语法变化,会报“Type of expression is ambiguous without more context”错误,可使用如下代码。标注的表示修改的地方)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import UIKit
CoreMotion
class ViewController : UIViewController ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, UIAccelerometerDelegate {
var ball: UIImageView !
speedX: UIaccelerationValue =0
speedY: =0
motionManager = cmmotionmanager ()
override func viewDidLoad() {
super .viewDidLoad()
//放一个小球在中央
ball= (image: UIImage (named: "ball" ))
ball.frame= CGRectMake ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,50)
ball.center= self .view.center
.view.addSubview(ball)
motionManager.accelerometerUpdateInterval = 1/60
if (motionManager.accelerometeravailable)
{
let queue = NSOperationQueue .currentQueue()
motionManager.startAccelerometerUpdatesToQueue(queue!,withHandler:
{ (accelerometerData : CMAccelerometerData ?,error: NSError ?) -> Void in
//动态设置小球位置
.speedX += accelerometerData!.acceleration.x
.speedY += accelerometerData!.acceleration.y
posX= .ball.center.x + CGFloat ( .speedX)
posY= .ball.center.y - .speedY)
//碰到边框后的反弹处理
posX<0 {
posX=0;
//碰到左边的边框后以0.4倍的速度反弹
.speedX *= -0.4
} else posX > .view.bounds.size.width {
.view.bounds.size.width
//碰到右边的边框后以0.4倍的速度反弹
.speedX *= -0.4
}
posY<0 {
posY=0
//碰到上面的边框不反弹
.speedY=0
posY> .view.bounds.size.height{
.view.bounds.size.height
//碰到下面的边框以1.5倍的速度反弹
.speedY *= -1.5
}
.ball.center= CGPointMake ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,posY)
})
}
}
}

原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_545.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐