一、搖一搖
直接監聽方法就可以
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.導入<CoreMotion/CoreMotion.h>框架
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];
}];