【问题标题】:How to change size of annotation on move the map?如何在移动地图时更改注释的大小?
【发布时间】:2018-02-18 14:37:06
【问题描述】:
我正在使用 swift 开发 iOS 应用程序。该应用程序包含一个带有自定义注释视图的 MKMapView。我想根据到视图中心的距离更改注释的大小。我知道我可以使用 view.transform = CGAffineTransform(scaleX: transformFaktor, y: transformFaktor) 之类的东西,但我不知道在哪里放置它以及如何从注释中获取视图。
【问题讨论】:
标签:
swift
mkmapview
mkannotation
mkannotationview
【解决方案1】:
您可以使用UIGraphics 更改注释的大小。
例如
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
}