iphone ios UIAccelerometer教程/用法
来源:互联网 发布:新理念外语网络教学 编辑:程序博客网 时间:2024/05/21 06:51
希望各位能支持一下我们的网站:http://t.cn/zOdAUxJ 提提意见
转载一篇老外的文章
If you like this tutorial, feel free to send me a donation at baseballer1149@aol.com via paypal.
First off, the UIAccelerometer is something that a lot of apps use. It's that thing that automatically detects when the user has moved his/her iphone. It measures on 3 axis, and they are the following.
X: When the user moves iphone left/right
Y: When the user moves iphone forward/backward
Z: When the user moves the iphone up/down
Now, to use this in an application.
Go ahead and open up a new View-based application.
Lets name is accel.
Now, open up accelViewController.h and add an IBOutlet for a UILabel
Code:
// code@interface accelViewController : UIViewController < UIAccelerometerDelegate >{IBOutlet UILabel *label;}@property (nonatomic, retain) IBOutlet UIlabel *label;// end of code
Code:
@synthesize label;
now, open up the .nib file for this view.
Drag on a UILabel, and in connections inspector drag from "New Referencing Outlet" to files owner, and select the label outlet.
Now, head back to the *.m file
uncomment the viewDidLoad method (towards the bottom of the file)
And inside of the viewDidLoad method, put the following:
Code:
[label setText:@"fixed"]UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];accel.delegate = self;accel.updateInterval = 1.0f/60.0f;
Code:
[label setText:@"fixed"];
Code:
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
Code:
accel.delegate = self;accel.updateInterval = 1.0f/60.0f;
Increasing that value 1.0f/60.0f may put strain on the battery
Now, all thats left is to implement the accelerometer:didAccelerate method
somewhere in the file, add this method:
Code:
- (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler {if (fabsf(aceler.x) > 1.5 || fabsf(aceler.y) > 1.5 || fabsf(aceler.z) > 1.5){if ([label.text isEqualToString:@"Broken"] == YES){[label setText:@"Fixed"];}else{[label setText:@"Broken"];} }
Have fun and post comments if you need help
Oh, and don't forget to build-run the app!
- iphone ios UIAccelerometer教程/用法
- iOS UIAccelerometer
- ios 加速传感器UIAccelerometer
- [ios2][转]iOS摇动检测 (UIAccelerometer)
- iOS内置加速计(UIAccelerometer/CoreMotion)
- iOS内置加速计(UIAccelerometer/CoreMotion)
- iphone ios layer 教程应用
- iphone ios gcc 编译 教程
- iphone ios layer 教程应用
- iphone ios opengles 学习教程
- iphone ios layer 教程应用
- iphone ios layer 教程应用
- iphone ios layer 教程应用
- iphone ios layer 教程应用
- iphone ios NSNumberFormatter用法示例
- IOS学习笔记-加速度传感器(重力感应)-UIAccelerometer
- IOS学习笔记-加速度传感器(重力感应)-UIAccelerometer
- iphone开发教程(1) iOS大纲
- JSON对象和字符串之间的相互转换
- Matlab画图详解
- 所有的支付平台
- 用RadioButton实现选项卡效果并设置样式
- Unix纪元秒转换为日期的JavaScript实现
- iphone ios UIAccelerometer教程/用法
- return闭包函数
- jdbc连接数据库sql2000(myeclipse database explorer)
- hash表的用法!(为晚上的百度电话面试做最后的准备~~)
- linux进程管理
- Arrays和Collections的sort方法
- axis2+webService的入门例子(server)
- asp.net 截取html标签取对应值
- Linux软件之——腾讯QQ