当前位置 : 主页 > 手机开发 > ios >

ios – 检测GMSMapView缩放

来源:互联网 收集:自由互联 发布时间:2021-06-11
有没有办法在这个Google Map Services组件中检测缩放(捏合和双击)? - (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture 以上方法无论动作如何都会发生火灾. 还有另一种方法可以检测何时缩放
有没有办法在这个Google Map Services组件中检测缩放(捏合和双击)?

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture

以上方法无论动作如何都会发生火灾.

还有另一种方法可以检测何时缩放(或任何其他属性)已经改变 – 键值观察(又名KVO).当没有为我们提供的委托方法时,它特别有用.来自Apple docs:

Key-value observing provides a mechanism that allows objects to be
notified of changes to specific properties of other objects.

无论您在何处设置地图视图,请添加以下代码段:

[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"];

}

快乐的编码:-)

网友评论