注释canShowCallout在自定义类中不起作用

时间:2020-11-12 18:57:58

标签: swift xcode

在“自定义”类注释中,无法使用显示标注。为什么不显示标注?有时它可以工作,但是我不知道它依赖什么。可能是什么原因?我究竟做错了什么 ?我还需要补充什么?非常感谢您的帮助...

这是我的代码:

class MapPinView: MKAnnotationView {
    private lazy var containerView: UIView = {
        let view = UIView(frame: CGRect(x: -12, y: -4, width: 25, height: 25))
        view.layer.cornerRadius = 5.0
        canShowCallout = true
        
        return view
    }()
    
    private lazy var imageView: UIImageView = {
        let imageview = UIImageView()
        imageview.translatesAutoresizingMaskIntoConstraints = false
        imageview.image = UIImage(named: "point8")
        imageview.layer.cornerRadius = 5.0
        imageview.clipsToBounds = true
        
        return imageview
    }()
    
    private lazy var bottomCornerView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .white
        view.layer.cornerRadius = 7.0
        return view
    }()
    
    // MARK: Initialization
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        setupView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupView() {  
        let angle = (39.0 * CGFloat.pi) / 180
        let transform = CGAffineTransform(rotationAngle: angle)
        bottomCornerView.transform = transform
        
        addSubview(containerView)
        containerView.addSubview(imageView)
        imageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 4.0).isActive = true
        imageView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 4.0).isActive = true
        imageView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -4.0).isActive = true
        imageView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -4.0).isActive = true
    }
}

    override func viewDidLoad() {
          super.viewDidLoad(
          mapView.delegate = self
          self.mapView.register(MapPinView.self, forAnnotationViewWithReuseIdentifier: "Annotation")
        
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let identifier = "Annotation"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "Annotation", for: annotation)
        annotationView = MapPinView(annotation: annotation, reuseIdentifier: "Annotation")
          let detailAnnotation = annotation as! WaypointsAnnotation
          if (detailAnnotation.type == "WP") {   
              annotationView.isEnabled = true
              annotationView.canShowCallout = true
              annotationView.annotation = annotation
          }
}

0 个答案:

没有答案