ios - 检测 GMSMapView 缩放

标签 ios iphone objective-c google-maps gmsmapview

有没有办法检测此 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/

相关文章:

ios - 了解用于分配的 xcode 工具

iphone - 是否有一个通用的媒体查询来设置所有 iphone 4、4s、5 的样式

ios - SwiftUI中的.transition修饰符

在 iOS 自定义键盘中使用时不支持 iOS CoreBluetooth 状态

swift - 如何在存储在数组/Swift 5 中的 View 中显示所有 TextField

iphone - 当我在模拟器上运行iPhone应用程序时,屏幕为白色

java - 在 Obj-C 中实现类似 Java 的 ByteBuffer 的最佳方法是什么?

objective-c - 延迟 XCTest 中所有单元测试的执行

objective-c - 如何为 iOS 创建二维码阅读器

ios - AFNetworking 下载队列