我正在处理以下代码,其中包含一个人行走的步骤。但每当我从一个视图走到另一个视图时,计数器就会停止。我正在使用SOMotionDetector。
注意:打印到控制台时,即使更改视图也会继续运行。
#import "SOMotionDetector.h"
#import "SOStepDetector.h"
@interface ContarPasos ()<SOMotionDetectorDelegate>
{
int stepCount;
}
@property (weak, nonatomic) IBOutlet UILabel *speedLabel;
@property (weak, nonatomic) IBOutlet UILabel *stepCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *motionTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *isShakingLabel;
@end
@implementation ContarPasos
- (void)viewDidLoad
{
[super viewDidLoad];
__strong ContarPasos *weakSelf = self;
[SOMotionDetector sharedInstance].motionTypeChangedBlock = ^(SOMotionType motionType) {
NSString *type = @"";
switch (motionType) {
case MotionTypeNotMoving:
type = @"No estas moviendote";
break;
case MotionTypeWalking:
type = @"Caminando";
break;
case MotionTypeRunning:
type = @"Corriendo";
break;
case MotionTypeAutomotive:
type = @"Automotive";
break;
}
weakSelf.motionTypeLabel.text = type;
};
[SOMotionDetector sharedInstance].locationChangedBlock = ^(CLLocation *location) {
weakSelf.speedLabel.text = [NSString stringWithFormat:@"%.2f km/h", [SOMotionDetector sharedInstance].currentSpeed * 3.6f];
};
[SOMotionDetector sharedInstance].accelerationChangedBlock = ^(CMAcceleration acceleration) {
BOOL isShaking = [SOMotionDetector sharedInstance].isShaking;
weakSelf.isShakingLabel.text = isShaking ? @"Corriendo":@"No corriendo";
};
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
[SOMotionDetector sharedInstance].useM7IfAvailable = YES; //Use M7 chip if available, otherwise use lib's algorithm
}
[[SOMotionDetector sharedInstance] startDetection];
[[SOStepDetector sharedInstance] startDetectionWithUpdateBlock:^(NSError *error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
return;
}
stepCount++;
weakSelf.stepCountLabel.text = [NSString stringWithFormat:@"Pasos: %d", stepCount];
NSLog(@"always printed on console but not in the view: %d", stepCount);
}];
}