实体机测试加速计
2013-03-16 16:22
351 查看
Reading Accelerometer Data
There are three accelerometers in the iPhone, one for each axis. The X-axis reads from left to right, the Y-axis running up and down and the Z-axis for acceleration from back tofront.
To read this data you need to call the object UIAccelerometer, you don’t have yo create it nor destroy it, just use it. It contains all the information you need from the accelerometer.
Lets create an interface to display this data.
Double click the AccelerometerViewController.xib in the resources folder and drag to the view three labels and a round rect button.
Double click and erase the text of the labels, then save and return to Xcode.
Now we have to create the code for the labels. Click on AccelerometerViewController.h in the “Clases folder”. Right before the opening curly brace of the @interface add to let it
know we are going to use the accelerometer data in this view, then declare the three atributes of the class to appear in the labels, like this:
@interface AccelerometerViewController : UIViewController { UILabel *x; UILabel *y; UILabel *z; } @property (nonatomic, retain) IBOutlet UILabel *x; @property (nonatomic, retain) IBOutlet UILabel *y; @property (nonatomic, retain) IBOutlet UILabel *z; @end
We are done here. Go to the implementation (AccelerometerViewController.m). First of all don’t forget to @synthesize x, y, z;. Then find the method “viewDidLoad” and uncomment it.
Before the [super viewDidLoad] statement create an object and assign it the [UIAccelerometer sharedAccelerometer], with this we are creating a reference to the accelerometer data. Then set the accelerometer delegate to the view and the update interval for
the values to ten times per second. The method should look like this:
- (void)viewDidLoad { UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer]; accel.delegate = self; accel.updateInterval = 1.0f/10.0f; [super viewDidLoad]; }
At this point we have prepared the view to take the data from the accelerometer. Now we need to really take it. There is a method called “accelerometer didAccelerate”. This will
allow us to read the data and display it in the labels:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { x.text = [NSString stringWithFormat:@"X is: %f", acceleration.x]; y.text = [NSString stringWithFormat:@"Y is: %f", acceleration.y]; z.text = [NSString stringWithFormat:@"Z is: %f", acceleration.z]; }
The code is ready, the only thing left is to connect the variables to the interface.
Now let’s build and run. The iPhone simulator will show up and your app will run, it may look like you did something wrong because the data is not appearing, well, the problem is
that the simulator has no accelerometer, to test your app you need to deploy it to a real device, and in order to do so you need to pay at least $99.
But there is one thing the simulator can do, simulate rotation to landscape.
Switching to Landscape Mode
You may be wondering what was the round rect button for. I created it to demonstrate this part of the tutorial. If you build and run the app we just created and you press command+leftor righr arrow, the iPhone will switch to landscape mode, the problem is that our app wont.
Go to the “AccelerometerViewController.m” and find the method “shouldAutorotateToInterfaceOrientation” and uncomment it. Here we say what orientations our app will support. Replace
the method with this one:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) return YES; else { return NO; } }
With this we support rotation to landscape left and right. If you build and run you’ll se that the view now is switching to landscape mode but it isn’t adjusting very well to the
new size of the window.
One way to solve it is to create a new view and switch to that when the iPhone rotates. The other way is to change a few properties in the inspector window of interface builder,
the problem is that it doesn’t work for all the cases, sometimes you have to take the first option. In our case the second option works.
Double click the “AccelerometerViewController.xib” in the resources folder to open interface builder. Click the rounded button and select the “Size” tab of the Inspector Window.
In the section for “Autosizing” you’ll notice there is a square with two red lines perpendicular to the left side and the upper side. This means that the button is linked to left and the top of the view to maintain that distance to that sides.
Click the left side to uncheck it and select the right side. Now if you save and build and run the button should stick to the left.
There are other properties, like resizing. If you click the horizontal arrow inside the square and the line in the left outside the square, the button will resize maintaining a
proportion.
it will look like this:
Conclusion
The only way to master this is practicing. Make more iPhone apps and make them rotate.Stay tuned because we are getting closer to the end. The next stop is saving data.
相关文章推荐
- 使用 Watir 加速面向 Web 应用的自动化测试程序的开发
- javascript 加速title显示(未测试)
- PHP程序加速探索[2]--脚本执行速度测试.代码优化.压缩输出gzip
- FAQ_EJB3.0_junit测试操作实体bean出现的 not-null property references a null or transient value
- 使用 Watir 加速面向 Web 应用的自动化测试程序的开发
- “CDN加速”测试
- 使用 Watir 加速面向 Web 应用的自动化测试程序的开发
- 源码编译LAMP架构,phpMyadmin管理数据库,增加xcache加速功能,通过ab测试效果。
- LAMP分布式架构,通过fcgi通信方式进行工作详解和缓存加速之压力测试。
- 《Entity Framework 6 Recipes》中文翻译系列 (28) ------ 第五章 加载实体和导航属性之测试实体是否加载与显式加载关联实体
- Flash Player 10.1 beta GPU 加速能力测试
- 基于半边数据结构(翼边数据结构)的Eular操作来实现扫成 通过OpenGL进行CAD实体建模 经测试可直接运行
- MVC2测试强类型实体校验
- PHP程序加速探索[2]--脚本执行速度测试.代码优化.压缩输出gzip
- 用stage3d加速2d arpg游戏的性能测试和处理思路
- SpringSecurity下做POST测试以及传递实体
- GPU加速MG整车设计平台汽车碰撞测试高性能计算
- 测试WWW方案(反向代理,负载均衡,HTTP加速缓存)
- intel GPU加速在linux 上测试, 多路编码
- 自己做的加速app测试流程的小工具,目前打算开放使用,想注册的朋友抓紧了,嘻嘻