您的位置:首页 > 其它

简单的计步器实现

2016-08-10 21:08 260 查看
#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;
}


效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  计步器