Rya*_*yan 5 counter objective-c ios core-motion apple-m7
我想知道是否有人可以向我展示如何实施CMStepCounter的示例.(我查看了文档,但仍然对如何实现有点困惑).
我想在每次采取步骤时更新我的视图上的UILabel.我也希望让应用程序在关闭时继续计算步数.
我对iOS比较新,任何帮助都会非常感激:)!
谢谢,瑞恩
您应该按如下方式实现它
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *stepsCountingLabel; // Connect this outlet to your's label in xib file.
@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];
}];
}];
}
}
- (void)updateStepCounterLabelWithStepCounter:(NSInteger)countedSteps
{
self.stepsCountingLabel.text = [NSString stringWithFormat:@"%ld", (long)countedSteps];
}
@end
Run Code Online (Sandbox Code Playgroud)
但请注意,有时startStepCountingUpdatesToQueue的块会延迟更新numberOfSteps.
归档时间: |
|
查看次数: |
4225 次 |
最近记录: |