如何更改MKMarkerAnnotationView
大小?
我尝试设置annotationView.bounds.size = CGSize(width: 50, height: 50)
,但看起来大小没有改变。我还尝试打印视图的大小,看起来它默认为28,28
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { return nil }
let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: Constant.Indentifier.mapPoint)
annotationView.canShowCallout = true
annotationView.animatesWhenAdded = true
annotationView.glyphImage = UIImage(systemName: "house.fill")
annotationView.glyphTintColor = .systemBlue
annotationView.markerTintColor = .white
print(annotationView.bounds.size) // defaulted to 28,28
annotationView.bounds.size = CGSize(width: 50, height: 50) // Does not change bubble size
return annotationView
}
请参见glyphImage
documentation,它讨论字形的大小:
标志符处于正常状态时显示字形图像。将字形图像创建为模板图像,以便可以将字形色调颜色应用到其上。通常,您在iOS上将此图像的大小设置为20 x 20点,在tvOS上设置为40 x 40点。但是,如果未在
selectedGlyphImage
属性中提供单独的选定图像,请在iOS上将此图像的大小设置为40 x 40点,在tvOS上为60 x 40点。 MapKit可以缩放大于或小于这些尺寸的图像。
最底线,selectedGlyphImage
的两个状态(选定和未选定)都有固定大小。
如果想要更大的注释视图,则需要编写自己的MKMarkerAnnotationView
。例如,仅创建一个大房子图像相对容易:
MKAnnotationView
顺便说一句,我建议像下面这样注册此批注视图类,然后完全删除class HouseAnnotationView: MKAnnotationView {
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
let configuration = UIImage.SymbolConfiguration(pointSize: 50)
image = UIImage(systemName: "house.fill", withConfiguration: configuration)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
方法。
mapView(_:viewFor:)
现在,以上注释视图仅呈现大的“房屋”图像。如果您想像mapView.register(HouseAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
一样将其放在气泡中,则必须自己绘制:
MKMarkerAnnotationView
产生:
class HouseAnnotationView: MKAnnotationView {
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
configureImage()
configureView()
configureAnnotationView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private extension HouseAnnotationView {
func configureImage() {
let radius: CGFloat = 25
let center = CGPoint(x: radius, y: radius)
let rect = CGRect(origin: .zero, size: CGSize(width: 50, height: 60))
let angle: CGFloat = .pi / 16
let image = UIGraphicsImageRenderer(bounds: rect).image { _ in
UIColor.white.setFill()
let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: .pi / 2 - angle, endAngle: .pi / 2 + angle, clockwise: false)
path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))
path.close()
path.fill()
let configuration = UIImage.SymbolConfiguration(pointSize: 24)
let house = UIImage(systemName: "house.fill", withConfiguration: configuration)!
.withTintColor(.blue)
house.draw(at: CGPoint(x: center.x - house.size.width / 2, y: center.y - house.size.height / 2))
}
self.image = image
centerOffset = CGPoint(x: 0, y: -image.size.height / 2) // i.e. bottom center of image is where the point is
}
func configureView() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowRadius = 5
layer.shadowOffset = CGSize(width: 3, height: 3)
layer.shadowOpacity = 0.5
}
func configureAnnotationView() {
canShowCallout = true
}
}
但是即使那样,也不能重现所有的行为。因此,这全都取决于您需要多少MKMarkerAnnotationView
行为/外观以及拥有更大的注释视图是否值得所有这些努力。