如何在移动地图时更改注释的大小?

时间:2018-02-18 14:37:07

标签: swift mkmapview mkannotation mkannotationview

我正在使用swift开发iOS应用。该应用程序包含一个带有自定义注释视图的MKMapView。我想根据到视图中心的距离来更改注释的大小。我知道我可以使用类似view.transform = CGAffineTransform(scaleX: transformFaktor, y: transformFaktor)的内容,但我不知道放置它的位置以及如何从注释中获取视图。

1 个答案:

答案 0 :(得分:0)

您可以使用UIGraphics更改注释的大小。

e.g。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let image = UIImage(named: "Image name here")
    let resizedSize = CGSize(width: 100, height: 100)

    UIGraphicsBeginImageContext(resizedSize)
    image?.draw(in: CGRect(origin: .zero, size: resizedSize))
    let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "Pin")
    annotationView.image = resizedImage
    return annotationView
}