在标记上方显示自定义视图

时间:2019-06-24 09:01:39

标签: ios swift mapbox

点击标记后,如何在标记上方显示CustomCalloutView

使用这种方法交易

func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedRect: CGRect, animated: Bool) {

    if isCalloutTappable() {
        let gesture = UITapGestureRecognizer(target: self, action: #selector(calloutTapped))
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(gesture)
    } else {
        self.isUserInteractionEnabled = false
    }

    //Always, Slightly above center
    self.center = view.center.applying(CGAffineTransform(translationX: 0, y: -self.frame.height))
    view.addSubview(self)

}

如何将标记框传递给它?

1 个答案:

答案 0 :(得分:2)

解决方案:

CustomCalloutView

var annotationPoint: CGPoint

required init(annotation: CustomAnnotation, annotationPoint: CGPoint) {
    self.representedObject = annotation
    self.annotationPoint = annotationPoint
}


func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedRect: CGRect, animated: Bool) {

    self.center = annotationPoint.applying(CGAffineTransform(translationX: 0, y: -self.frame.height - 40.0))

    view.addSubview(self)

}

MapVC中:

 func mapView(_ mapView: MGLMapView, calloutViewFor annotation: MGLAnnotation) -> MGLCalloutView? {        
    let customAnnotation = CustomAnnotation(coordinate: annotation.coordinate, title: title, subtitle: subtitle, image: img!)

    let annotationPoint = mapView.convert(annotation.coordinate, toPointTo: nil)
    return CustomCalloutView(annotation: customAnnotation, annotationPoint: annotationPoint)
 }