0
我正在编写一个iOS应用程序,用于统计用户在使用按钮激活时所采取的步骤。我现在能够计算这些步骤,但我希望能够通过用户请求暂停和重置步骤计数器。我对XCode并不是很有经验,所以可能有一种简单的方法来做到这一点。我用#2类似于一个代码可供选择:在iOS Core Motion中暂停,恢复和重置步骤计数
#import "ViewController.h"
#import "DTStepModelController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController()
@property (weak, nonatomic) IBOutlet UILabel *stepsCountingLabel; @property (nonatomic, strong) CMStepCounter *cmStepCounter;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@end
@implementation ViewController
{
DTStepModelController *_stepModel;
}
- (NSOperationQueue *)operationQueue
{
if (_operationQueue == nil)
{
_operationQueue = [NSOperationQueue new];
}
return _operationQueue;
}
- (void)updateStepCounterLabelWithStepCounter:(NSInteger)countedSteps
{
self.stepsCountingLabel.text = [NSString stringWithFormat:@"%ld", (long)countedSteps];
}
- (IBAction)StartCountingSteps:(id)sender {
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];
}];
}];
}
}
任何了解,或suggessions?