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;
@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
git pull —rebase //origin->master, git下载,同时执行变基操作
要获取该源码,可在终端输入:
git clone [email protected]:lihux/lihuxCoreMotionTest.git