CMStepCounter を実装する方法の例を誰かに見せてもらえないかと思っていました。(私はドキュメントを見てきましたが、実装方法についてはまだ少し混乱しています)。
ステップが実行されるたびに、View の UILabel を更新しようとしています。また、アプリを閉じたときに歩数をカウントし続けることも検討しています。
私はiOSに比較的慣れていないので、どんな助けでも大歓迎です:)!
ありがとう、ライアン
CMStepCounter を実装する方法の例を誰かに見せてもらえないかと思っていました。(私はドキュメントを見てきましたが、実装方法についてはまだ少し混乱しています)。
ステップが実行されるたびに、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 の更新を遅らせる場合があることに注意してください。