简单的计步器实现

#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;
}

效果图

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦