CAL001-CoreMotion实战

来源:互联网 发布:中金佳成 知乎 编辑:程序博客网 时间:2024/06/06 21:10
     参考文档: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:


@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


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

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

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

git clone git@github.com:lihux/lihuxCoreMotionTest.git


     
0 0
原创粉丝点击