您的位置:首页 > 其它

iPhone计步器

2016-02-08 20:37 381 查看
//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: