更改MKMarkerAnnotationView大小 [英] Change MKMarkerAnnotationView size

查看:31
本文介绍了更改MKMarkerAnnotationView大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改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
}

推荐答案

参见glyphImagedocumentation,其中讲述了字形的大小:

当标记处于正常状态时显示字形图像。将字形图像创建为模板图像,以便可以对其应用字形色调颜色。通常,您在IOS上将此映像的大小设置为20x20点,在TVOS上将其大小设置为40x40点。但是,如果您没有在selectedGlyphImage属性中提供单独的选定映像,则在IOS上将此映像的大小设为40乘40磅,在TVOS上设为60乘40磅。MapKit缩放大于或小于这些大小的图像。

底线是,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行为。因此,这一切都取决于您需要多少MKMarkerAnnotationView行为/外观,以及拥有更大的注释视图是否值得您付出所有的努力。

这篇关于更改MKMarkerAnnotationView大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆