iPhone计步器

来源:互联网 发布:淘宝短信营销资料 编辑:程序博客网 时间:2024/05/18 07:40
////  ViewController.m#import "ViewController.h"#import <CoreMotion/CoreMotion.h>@interface ViewController ()/** 计步器对象 */@property (nonatomic, strong) CMStepCounter *counter;@property (weak, nonatomic) IBOutlet UILabel *stepLabel;@end@implementation ViewController#pragma mark - 懒加载代码- (CMStepCounter *)counter{    if (_counter == nil) {        _counter = [[CMStepCounter alloc] init];    }    return _counter;}- (void)viewDidLoad {    [super viewDidLoad];        // 1.判断计步器是否可用    if (![CMStepCounter isStepCountingAvailable]) {        NSLog(@"计步器不可用");        return;    }        // 2.开始计步    [self.counter startStepCountingUpdatesToQueue:[NSOperationQueue mainQueue] updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error) {        if (error) return;                self.stepLabel.text = [NSString stringWithFormat:@"您一共走了%ld步", numberOfSteps];    }];}@end

0 0
原创粉丝点击