本文實例為大家分享了iOS計步器實現代碼,供大家參考,具體內容如下
#import "ViewController.h"#import <CoreMotion/CoreMotion.h>@interface ViewController ()@property (nonatomic, strong) CMStepCounter *conter;@property (weak, nonatomic) IBOutlet UILabel *stepLabel;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 1、判斷計步器是否可用 if (![CMStepCounter isStepCountingAvailable]) {return;} // 2、開始計步 [self.conter startStepCountingUpdatesToQueue:[NSOperationQueue mainQueue] updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) { self.stepLabel.text = [NSString stringWithFormat:@"一共走了%ld步", numberOfSteps]; }];}- (CMStepCounter *)conter{ if (_conter == nil) { _conter = [[CMStepCounter alloc] init]; } return _conter;}
效果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答