CAL001-CoreMotion实战

     参考文档: https://www.inkling.com/read/learning-ios-programming-alasdair-allan-2nd/chapter-9/the-core-motion-framework
     lihux原创程序git地址: https://github.com/lihux/lihuxCoreMotionTest  
     实战:
     1. Xcode新建一个空工程:LihuxCoreMotionTest;
     2.工程配置文件中添加CoreMotion.framework框架;
     3.新建一个继承自UIViewController的VC:LHCoreMotionViewController(创建时勾选上附带xib文件)
     5.LHCoreMotionViewController.m包含<CoreMotion/CoreMotion.h>头文件

     6.xib中添加UI,同时连接到LHCoreMotionViewController.m:

CAL001-CoreMotion实战_第1张图片

@property (weak, nonatomic) IBOutlet UILabel *xLabel;
@property (weak, nonatomic) IBOutlet UILabel *yLabel;
@property (weak, nonatomic) IBOutlet UILabel *zLabel;
@property (weak, nonatomic) IBOutlet UIProgressView *xBar;
@property (weak, nonatomic) IBOutlet UIProgressView *yBar;
@property (weak, nonatomic) IBOutlet UIProgressView *zBar;


     7.新增获取运动信息的成员变量:

@property (strong, nonatomic) CMMotionManager *motionManger;
@property (strong, nonatomic) NSOperationQueue *queue;

     8.采用pull的方式每个0.1秒获取一次运动信息(加速度):放入viewdidload方法中调用即可:

- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"CoreMotion框架测试20140109";
[self updateAccelerationInfomation];
}


- (void)updateAccelerationInfomation
{
self.motionManger = [[CMMotionManager alloc] init];
self.motionManger.accelerometerUpdateInterval = TENHZ;
if (self.motionManger.accelerometerAvailable) {
NSLog(@"Accelermeter availaber");
self.queue = [NSOperationQueue currentQueue];
[self.motionManger startAccelerometerUpdatesToQueue:self.queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
CMAcceleration acceleration = accelerometerData.acceleration;

self.xLabel.text = [NSString stringWithFormat:@"%.5f", acceleration.x];
self.xBar.progress = ABS(acceleration.x);
self.yLabel.text = [NSString stringWithFormat:@"%.5f", acceleration.y];
self.yBar.progress = ABS(acceleration.y);
self.zLabel.text = [NSString stringWithFormat:@"%.5f", acceleration.z];
self.zBar.progress = ABS(acceleration.z);
}];
}

}
    10.大功告成运行截图(因为是要使用传感器,所以必须真机测试才行):真机为iphone4s+ios6

CAL001-CoreMotion实战_第2张图片
   扩展:将新建的工程同步到自己的github账号中去,便于以后管理:
     1.进入工程文件夹,创建git管理:
     >>git init //初始化git
     >>git add . //将当前工程所有文件添加到git暂存区中
     >>git commit -m “开天辟地” //提交首个commit
     2.将本地git管理的工程同步到GitHub账号中去:
     >>登陆GitHub(如没有账号可先注册一个);
     创建一个新的仓库(repository),按照提示提交文件即可:
     git remote add origin [email protected]:linux/lihuxCoreMotionTest.git
     git push -u origin master    //master->origin,文件上传

     git  pull —rebase //origin->master, git下载,同时执行变基操作

    要获取该源码,可在终端输入:

git clone [email protected]:lihux/lihuxCoreMotionTest.git


     

你可能感兴趣的:(CAL001-CoreMotion实战)