我正在尝试创建一个可调整大小的注释
我创建了一个自定义注释
class MapAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var image: UIImage? = UIImage(named: "image")
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}
和包含图像的annotationView。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? MapAnnotation {
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier("myCustomAnnotation")
if pin == nil {
pin = MKAnnotationView(annotation: annotation, reuseIdentifier: "myCustomAnnotation")
}
pin!.image = annotation.image
return pin
}
return nil
}
当我调整 mapView 的大小时,我希望 MKAnnotationView 也会调整大小。
我知道 MKOverlayRenderer 有 drawMapRect(mapRect: MKMapRect, ZoomScale: MKZoomScale, inContext context: CGContext)
函数,其中包含 ZoomScale: MKZoomScale 参数。
此外我看到了这个解决方案:
let zoomScale: MKZoomScale = mapView.bounds.size.width /
CGFloat(mapView.visibleMapRect.size.width)
但我想要访问 drawMapRect
函数中的 MKZoomScale
,而不是自定义方式。
这意味着我需要制作一个 MKOverlayRenderer 而不是我不想制作的 MKAnnotationView,因为 MKOverlay 正在 map 上绘制,而我想制作一个annotationView,但它没有绘制在 map 上。
之后,我将拥有 MKZoomScale,我想在 MKZoomScale 1 到 0.25 之间调整annotationViews 的大小,如果 MKZoomScale 低于 0.25,我希望annotationViews 将消失。
感谢您的帮助!
最佳答案
您可以从mapView的区域变量的范围中获取“缩放”量:
mapView.region.span
然后,在您的 viewForAnnotation
函数中,您可以进行一些数学运算来缩放注释,或者让注释对自身执行某种函数以显示更大。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
<pin init methods>
let averageSpan = mapView.region.span.x * mapView.region.span.x * 0.5
let pinScale = someFunctionToReturnScaleBasedOnAverageSpan(averageSpan)
pin.scaleSelf(pinScale)
return pin
}
我不记得了,但是如果在 map 更新其跨度时没有调用viewForAnnotation
,则需要某种删除和添加注释的更新函数.
但是,使用 MKOverlay 会更容易。
关于ios - 使用 MKZoomScale 调整 MKAnnotationView 的大小 - Swift 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39680292/