开发手册 欢迎您!
软件开发者资料库

iOS - 加速度计

iOS Accelerometer - 从基本到高级概念的简单简单步骤,在iOS上学习iPhone和iPad应用程序开发,包括入门,环境设置,Objective-C,首个iPhone应用程序,操作和插座,代表,UI元素,加速度计,通用应用程序,相机管理,位置处理,SQLite数据库,发送电子邮件,音频和视频,文件处理,访问地图,应用程序内购买,iAd集成,GameKit,故事板,自动布局,Twitter和Facebook,内存管理,应用程序调试。

加速度计用于检测设备在x,y和z三个方向上位置的变化.我们可以知道设备相对于地面的当前位置.要测试此示例,您需要在设备上运行它,并且无法在模拟器上运行.

加速度计 - 涉及的步骤

第1步 : 创建一个简单的基于视图的应用程序.

步骤2 : 在 ViewController.xib 中添加三个标签,并创建ibOutlets,将它们命名为xlabel,ylabel和zlabel.

第3步 : 更新ViewController.h如下 :

  #import< UIKit/UIKit.h>  @interface ViewController:UIViewController< UIAccelerometerDelegate> { IBOutlet UILabel * xlabel;  IBOutlet UILabel * ylabel;  IBOutlet UILabel * zlabel; }  @end

第4步 : 更新 ViewController.m 如下 :

  #import"ViewController.h" @interface ViewController() @end  @implementation ViewController   - (void)viewDidLoad { [super viewDidLoad];  [[UIAccelerometer sharedAccelerometer] setDelegate:self]; //加载视图后进行任何其他设置,通常来自nib }   - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; //处理可以重新创建的任何资源. }   - (无效)加速度计:(UIAccelerometer *)加速度计didAccelerate:(UIAcceleration *)acceleration { [xlabel setText:[NSString stringWithFormat:@" %F",acceleration.x]];  [ylabel setText:[NSString stringWithFormat:@"%f",acceleration.y]];  [zlabel setText:[NSString stringWithFormat:@"%f",acceleration.z]]; }  @end

输出

当我们在中运行应用程序时iPhone 设备,我们将获得以下输出 :

iOS Tutorial