有没有办法检测此 Google map 服务组件中的缩放(捏和双击)?
- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture
无论移动如何,上述方法都会触发。
最佳答案
还有另一种方法可以检测缩放(或与此相关的任何其他属性)是否已更改 - Key-Value-Observing(又名 KVO)。当没有提供给我们使用的委托(delegate)方法时,它特别有用。来自苹果docs :
Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects.
无论你在哪里设置你的 map View ,添加这个片段:
[self.mapView addObserver:self forKeyPath:@"camera.zoom" options:0 context:nil];
现在您只需实现 -observeValueForKeyPath:ofObject:change:context:
方法即可实际接收回调。像这样:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"camera.zoom"]) {
// this static variable will hold the last value between invocations.
static CGFloat lastZoom = 0;
GMSMapView *mapView = (GMSMapView *)object;
CGFloat currentZoom = [[mapView camera] zoom];
if (!(fabs((lastZoom) - (currentZoom)) < FLT_EPSILON)) {
//Zoom level has actually changed!
NSLog(@"Zoom changed to: %.2f", [[mapView camera] zoom]);
}
//update last zoom level value.
lastZoom = currentZoom;
}
}
不要忘记根据需要在 -dealloc
或 -viewDidDissapear
中移除观察者:
- (void)dealloc {
[self.mapView removeObserver:self forKeyPath:@"camera.zoom"];
}
快乐编码:-)
关于ios - 检测 GMSMapView 缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734831/