转载自:http://www.bit-101.com/blog/?p=1824
For the final installment in this series, we’ll add accelerometer support to the ball, allowing you to tilt the device to change the direction of “down”. Note, although you can rotate the device in the iPhone simulator, this does not simulate acceleration.
The only way you’ll be able to see this in action is on a physical device. This means that you’ll have to pay $99 sign up for and be accepted to the iPhone Developer program, and figure out how to provision apps to your device. I’m not going to cover that
here, so I’ll assume that you’ve figured that out on your own. OK, let’s jump in.
So far, we have gravity in the ball, affecting its y velocity. If we know which way the device is tilted, we can change that to affect the velocity on the x axis as well, proportional to the degree of tilt. For example, if you tilted the device 90 degrees to
the left, gravity should only affect the x velocity. If you turn it upside down, it will affect only the y velocity again, but in the opposite direction. In most cases, it will not be exactly straight, so there will be some x velocity and some y, proportional
to the angle of tilt.
To respond to acceleration, we need to do three things:
1. Have a class specify the UIAccelerometerDelegate protocol. This is similar to implementing an interface in ActionScript. But less strict. It’s telling the application that you expect this class to respond to acceleration, and it may have some of the methods
associated with this protocol. But unlike an interface, you are not required to implement all or even any of the methods.
2. Set the class as a delegate for accelerometer events. Think of this as adding an event listener.
3. Create a method to respond to the events. Here is where you will change gravity.
OK, step 1, specifying the protocol. We’ll have the Ball class do this directly, as that’s where gravity is. You just put the protocol name in angle brackets after the class name and and super classes, like so:
1
2
|
@interface
Ball : NSObject <UIAccelerometerDelegate> {
...
|
I’m not really sure what this does to be honest, as it seems you can be a delegate without specifying the protocol (although you will get a warning), and you can specify the protocol without being a delegate. But you’re supposed to do it, so do it.
Step 2. Set self as a delegate. We’ll do that in the init method in Ball.m:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
-
(id) init
{
self
= [super init];
if (self
!= nil) {
position
= CGPointMake(100.0, 100.0);
velocity
= CGPointMake(5.0, 5.0);
radius
= 20.0;
color
= [UIColor greenColor].CGColor;
bounce
= -0.9f;
gravity
= 0.5f;
dragging
= NO;
[[UIAccelerometer
sharedAccelerometer] setDelegate:self];
}
return self;
}
|
sharedAccelerometer is a class (static) property of the UIAccelerometer class, I assume it’s like getInstance of a Singleton. You are calling setDelegate on that object, passing self (this). Simple enough.
Step 3. Create a method which will be called on acceleration. Again, right in Ball.m:
1
2
3
|
-
( void )accelerometer:(UIAccelerometer
*)accelerometer didAccelerate:(UIAcceleration *)acceleration {
NSLog(@ "x:
%f, y: %f" ,
acceleration.x, acceleration.y);
}
|
You don’t need to declare this, as you specified the protocol, which, come to think of it, is probably why you set the protocol.
The method is called accelerometer and passes a pointer to a UIAccelerometer object, and a pointer to a UIAcceleration object. It’s this last one we’re interested in, as that will tell us the amount of tilt.
Here I’ve just put in a log statement so you can see that it’s working.
The acceleration parameter passed in will contain x, y, and z properties, which will range from -1.0 to +1.0. We’re only interested in x and y. x will be -1.0 when the device is tilted 90 degrees to the left, 0.0 when it’s upright and +1.0 when tilted to the
right. Likewise, y will be -1.0 when upright, and vary towards 0.0 on either side, and then to +1.0 when the device is upside down.
So, rather than a single value for gravity, we’ll need x and y values for gravity. We can store these in a point object. Declare it in Ball.h:
1
|
CGPoint
acceleratedGravity;
|
Leave gravity there, as that will affect the overall strength of gravity and generally stays the same. This new variable will change constantly according to tilt.
You can also initialize this in the init method:
1
|
acceleratedGravity
= CGPointMake(0.0, gravity);
|
Note that the x value is initialized to 0.0 and y to gravity, indicating no rotation.
Now we can change the acceleration method to alter this variable:
1
2
3
4
|
-
( void )accelerometer:(UIAccelerometer
*)accelerometer didAccelerate:(UIAcceleration *)acceleration {
acceleratedGravity.x
= acceleration.x * gravity;
acceleratedGravity.y
= -acceleration.y * gravity;
}
|
Note that the y value is reversed, otherwise the ball would fall “up”.
Finally, we just need to alter the update method in Ball.m to utilize acceleratedGravity instead of just gravity:
1
2
3
4
5
6
7
8
9
|
-
( void )update
{
if (dragging) return ;
velocity.x
+= acceleratedGravity.x;
velocity.y
+= acceleratedGravity.y;
position.x
+= velocity.x;
position.y
+= velocity.y;
...
|
And that’s it! Pretty simple actually. Now you tilt the device and the ball will always fall “down”. It will settle in a corner, or on the “top” of the device, or whatever direction is down. Amazing just how easy that was. Less than 10 lines of code in all.
And that brings us to the end of this series. Bear in mind that I’m a beginner at this stuff, and was writing this for my own selfish reasons – to learn the language and drill it into my head. I’ve already seen several places where the code in this project
is not up to snuff at all. I may go back and correct it at some point, but I’ll just say for now to take what I’ve written here as a jump start. It will get you up and running, but you really need to learn more about the right way to do things, like I am already
doing. And if you find something that indicates I did something incorrectly, it’s probably right.
分享到:
相关推荐
在Android开发中,`layout_gravity`和`gravity`是两个关键属性,它们都与控件的对齐和定位有关,但应用场景和作用对象不同。本文将深入探讨这两个属性的区别,以及它们在实际开发中的应用。 首先,我们来了解`...
"Android中gravity与layout_gravity的区别" Android 中的 gravity 和 layout_gravity 是两个常见的属性,它们都是用于设置视图组件的对齐方式,但是它们的作用域和应用场景却有所不同。 首先,让我们来看一下 ...
"Gravity" 在IT行业中,尤其是设计领域,通常指的是一个特定的字体或排版风格。这个标题可能是指一种设计资源,比如一套独特的字体库或者是一种图形设计软件中的字体选项。字体在数字媒体、印刷品、网站设计、应用...
《Go-Gravity:基于快照的Kubernetes打包与管理利器》 在当今的云原生时代,Kubernetes作为容器编排的领导者,已经成为了企业级应用部署的核心平台。然而,随着集群规模的扩大和复杂性的增加,管理和部署Kubernetes...
在Android开发中,`android:gravity` 是一个非常重要的属性,尤其在布局管理中起到关键作用。这个属性主要用于控制视图或布局内元素的位置和对齐方式,它可以帮助开发者精确地定位UI组件在屏幕上的位置。`android:...
本文将深入探讨“compass_revise.zip_correct for gravity_correct gravity_椭圆拟合圆心_磁力”这一主题,特别是针对磁力计和重力计的校正方法。我们将基于提供的MATLAB M文件“compass_revise.m”来解析这个过程。...
Gravity是一种由C++实现的轻量级、动态类型、可嵌入的编程语言,它以其对并发支持和类结构的设计,以及类似Swift的简洁语法,为开发者提供了高效且易用的脚本工具。在深入探讨Gravity之前,让我们先了解一下它的核心...
5. `calling_sequence.m`: 这个文件可能列出了整个程序的执行顺序,即哪些函数先调用,哪些后调用,这对于理解程序流程至关重要。 6. `recpos.m`: 根据名字推测,这个函数可能用于记录或更新物体的位置,特别是在...
全贯流式水轮机是一种适用于低水头水力发电的涡轮机设计,其特点在于转轮直径大,轴向布置,以及与发电机转子一体化的水轮机转轮。由于这类水轮机的水头较低,且转轮的直径较大,因此在其转轮内的流动会受到重力的...
尽管Flash技术在近年来逐渐被HTML5等新技术取代,但《Gravity_Master.swf》等经典Flash游戏仍然在许多玩家心中占有特殊地位。它们代表了一代人的网络娱乐记忆,也展示了游戏设计中的创新与乐趣。 总的来说,...
来源:www.fvalente.orgLicence:Unspecified平台:iOS设备:iPhone / iPad作者:Fernando Valente 一个测试iOS设备重力感应的例子。界面上有一个方块,随着设备位置的改变,界面上的方块会做出移动。其中利用...
This is an introduction to the basic tools of mathematics needed to understand the relation between knot theory and quantum gravity. The book begins with a rapid course on manifolds and differential ...
在“gravity sphere.rar”压缩包中,包含了一个用于计算球体重力的MATLAB程序,名为“gravity sphere”。这个程序可能涉及以下几个关键知识点: 1. **球体引力定律**:根据牛顿的万有引力定律,任何两个质点之间都...
Loops,Kknots, Gauge Theories and Quantum Gravity Loops,Kknots, Gauge Theories and Quantum Gravity Loops,Kknots, Gauge Theories and Quantum Gravity
android:gravity:设置的是控件自身上面的内容位置 android:layout_gravity:设置控件本身相对于父控件的显示位置。 看下如下代码段 代码如下:<?xml version=”1.0″ encoding=”utf-8″?><!– android:...
5. **陀螺仪数据**:陀螺仪用于测量设备的旋转速率,通过`startGyroUpdates`可以获取。结合加速度计数据,可以计算设备的姿态和运动轨迹。 6. **设备方向和姿态**:Core Motion框架中的`CMAttitude`类代表了设备的...
gravity 2D inversion
3D-gravity-engine.zip,使用3.js的行星和粒子三维引力引擎(WebGL库),3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他应用程序。
Gravity-Simulator, 在JS中,2D 牛顿重力模拟器 希望帮助或者修改这里项目?提交请求请求,它可能会被接受 !代码结构如何?我使用AMD模块系统构建应用程序,特别是我使用 require.js 作为模块加载程序有三个独立的...