5
我想知道是否有人能告訴我一個如何實現CMStepCounter的例子。 (我查看了文檔,但對於如何實現仍然有點困惑)。如何實現CMStepCounter CoreMotion - M7芯片
我期待每次採取措施時更新我View上的UILabel。我也希望讓應用程序在關閉時繼續計算步驟。
我比較新的iOS到任何幫助將不勝感激:) :)!
感謝, 瑞安
我想知道是否有人能告訴我一個如何實現CMStepCounter的例子。 (我查看了文檔,但對於如何實現仍然有點困惑)。如何實現CMStepCounter CoreMotion - M7芯片
我期待每次採取措施時更新我View上的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
然而值得注意的是,有時startStepCountingUpdatesToQueue的塊將延遲更新numberOfSteps你應該實現它。
謝謝,不過我注意到,應用程序似乎沒有更新應用程序關閉/手機被鎖定時所採取步驟的數量。有沒有辦法做到這一點 ?再次感謝。 – Ryan
您是否在應用程序的plist文件中指定了所需的背景模式? – ldindu
不,我找到了一個列表,但不確定選擇哪一個:)謝謝, – Ryan