iOS 摇一摇、计步器

一、摇一摇

直接监听方法就可以

1.用户开始摇晃手机

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event

2.摇一摇被打断(电话)

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event

3.摇一摇结束

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event

二、计步器

1.导入框架

2.初始化对象

self.counter=[[CMStepCounter alloc]init];

3.判断计步器是否可用

if(![CMStepCounter isStepCountingAvailable]) {

NSLog(@"计步器不可用");

return;

}

4.开始计步

[self.counter startStepCountingUpdatesToQueue:[NSOperationQueue mainQueue]updateOn:5 withHandler:^(NSInteger  numberOfSteps,NSDate *timestamp,NSError *error) {

if(error)return;

self.stepLabel.text= [NSStringstringWithFormat:@"您一共走了%ld步", numberOfSteps];

}];

你可能感兴趣的:(iOS 摇一摇、计步器)