想等一段时间再检查 CLLocationManager Speed 吗?
Want to wait a period of time before i check CLLocationManager's Speed again?
我目前正在使用委托函数 locationManager:didUpdateLocations
检查我的设备的速度。在发生事件时,我想等待 10 秒,然后再次检查我的设备的速度,但我不希望我的应用程序在这十秒内暂停,我希望我的设备仍在更新其速度,所以当我检查它时speed again after 10 seconds 它实际上是它在 10 秒后的速度。不仅仅是我开始等待 10 秒之前的速度。有人知道我应该怎么做吗?
试试这个:
您的速度正在更新,然后当您在 10 秒内记录它时,它会采用存储在 self.currentLocation
.
中的当前值
所以在我的例子中,10 秒的延迟在点击时有效:
- (void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations {
NSLog(@"current speed: %f", locations[0].speed);
CLLocation *location = [locations objectAtIndex:0];
self.currentLocation = location;
}
手势识别器:
UITapGestureRecognizer *logoTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logoTapped:)];
和事件处理程序:
- (void)logoTapped:(id)sender {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"SPEED AFTER 10 SECONDS OF WAIT: %f", self.currentLocation.speed);
});
}
我目前正在使用委托函数 locationManager:didUpdateLocations
检查我的设备的速度。在发生事件时,我想等待 10 秒,然后再次检查我的设备的速度,但我不希望我的应用程序在这十秒内暂停,我希望我的设备仍在更新其速度,所以当我检查它时speed again after 10 seconds 它实际上是它在 10 秒后的速度。不仅仅是我开始等待 10 秒之前的速度。有人知道我应该怎么做吗?
试试这个:
您的速度正在更新,然后当您在 10 秒内记录它时,它会采用存储在 self.currentLocation
.
所以在我的例子中,10 秒的延迟在点击时有效:
- (void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations {
NSLog(@"current speed: %f", locations[0].speed);
CLLocation *location = [locations objectAtIndex:0];
self.currentLocation = location;
}
手势识别器:
UITapGestureRecognizer *logoTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logoTapped:)];
和事件处理程序:
- (void)logoTapped:(id)sender {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"SPEED AFTER 10 SECONDS OF WAIT: %f", self.currentLocation.speed);
});
}