在这个Google地图服务组件中,有没有一种方法来检测缩放(挤压和双击)?
- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture上面的方法会触发,而不管所做的移动。
发布于 2015-05-26 19:43:33
还有另一种方法来检测何时缩放(或任何其他属性)是否发生了更改-键值观察(也称为KVO)。当没有委托方法可供我们使用时,它特别有用。来自苹果docs
键值观察提供了一种机制,允许在其他对象的特定属性发生更改时通知对象。
无论您在何处设置地图视图,请添加以下代码片段:
[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"];
}快乐编码:-)
发布于 2014-04-23 23:29:58
我希望您已经在头文件中使用了GMSMapViewDelegate
在作为GMSMapView对象委托的实现文件中使用以下代码
-(void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition*)position {
float zoom = mapView.camera.zoom;
// handle you zoom related logic
}发布于 2016-03-14 16:20:55
一些旧的,但仍然..。您可以通过这种方式进行检测,首先使用mapView来使用视图中的手势:
mapView.settings.consumesGesturesInView = true
for gestureRecognizer in mapView.gestureRecognizers! {
gestureRecognizer.addTarget(self, action: "handleMapGesture:")
}其次,在你的函数中,检查两件事,状态和触摸次数。
如果状态为.Changed,则手势开始,两次触摸即为缩放收缩。
难的是双击,你必须实现某种延迟监听并链接最后两个手势,识别“点击”的方法只有.Begin和.End以及一次触摸,这种手势没有.Changed状态。
注意:这在Swift 2上有效,在3或4上没有测试
https://stackoverflow.com/questions/22734831
复制相似问题