【发布时间】:2014-04-21 11:44:49
【问题描述】:
我正在尝试探索 CoreMotion 并且我正在尝试使用 CMStepCounter 类来获取步数。这是我如何实现我的view controller 来获得stepCounts
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *stepsCountingLabel;
@property (nonatomic, strong) CMStepCounter *cmStepCounter;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@end
@implementation ViewController
- (NSOperationQueue *)operationQueue
{
if (_operationQueue == nil)
{
_operationQueue = [NSOperationQueue new];
}
return _operationQueue;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([CMStepCounter isStepCountingAvailable])
{
self.cmStepCounter = [[CMStepCounter alloc] init];
[self.cmStepCounter startStepCountingUpdatesToQueue:self.operationQueue updateOn:1 withHandler:^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self updateStepCounterLabelWithStepCounter:numberOfSteps];
}];
}];
}
else
{
self.stepsCountingLabel.text = @"NO STEP COUNTING AVAILABLE";
}
}
- (void)updateStepCounterLabelWithStepCounter:(NSInteger)countedSteps
{
self.stepsCountingLabel.text = [NSString stringWithFormat:@"%ld", (long)countedSteps];
}
但是当我带着设备走路时,我没有得到任何更新,但是当我摇动设备时,它给了我一些随机的numberOfSteps 计数。如果我错过了什么,请告诉我。
【问题讨论】:
标签: ios iphone ios7 core-motion