//
// 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
iPhone计步器
最新推荐文章于 2025-11-18 16:55:09 发布
3381

被折叠的 条评论
为什么被折叠?



