浏览器家园
TAG标签|网站导航| 手机访问:m.liulanqi.com
加速度传感器是根据x、y和z三个方向来检测在设备位置的改变。
通过加速度传感器可以知道当前设备相对于地面的位置。
以下实例代码需要在真实设备上运行,在模拟器上是无法工作的。
1、创建一个简单的视图应用程序
2、在ViewController.xib中添加三个标签,并创建一个ibOutlets分别为:xlable、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]; //Do any additional setup after loading the view,typically from a nib } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)accelerometer:(UIAccelerometer *)accelerometer 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设备中运行该应用程序,得到的输出结果如下所示。
百度浏览器6.5版工具,百度浏览器6.5版推出全新工具,提升浏览体验!
Copyright 2019-2029 www.liulanqi.com 【浏览器家园】 版权所有
浏览器家园_下载浏览器就到浏览器家园 | 专注MAC浏览器和Windows浏览器下载和使用介绍
声明: 所有软件和文章收集整理来自互联网 如有异议 请与本站联系 本站为非赢利性网站 不接受任何赞助和广告