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.導入<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];

}];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容