简体   繁体   English

在Swift中为MKA注释自定义标注视图

[英]Customizing callout view for MKAnnotation in Swift

I need a completely customizable callout for for MKAnnotation . 我需要一个完全可定制的MKAnnotation标注。 I have subclassed MKAnnotationView and an UIView . 我有MKAnnotationViewUIView子类。 My code: 我的代码:

class CustomAnnotationView: MKAnnotationView {

    class var reuseIdentifier:String {
        return "CustomAnnotationView"
    }

    private var calloutView:MapPinCallOut?
    private var hitOutside:Bool = true

    var preventDeselection:Bool {
        return !hitOutside
    }


    convenience init(annotation:MKAnnotation!) {
        self.init(annotation: annotation, reuseIdentifier: CustomAnnotationView.reuseIdentifier)
        //false?
        canShowCallout = true;
    }

    override func setSelected(selected: Bool, animated: Bool) {
        let calloutViewAdded = calloutView?.superview != nil

        if (selected || !selected && hitOutside) {
            super.setSelected(selected, animated: animated)
        }

        self.superview?.bringSubviewToFront(self)

        if (calloutView == nil) {
            calloutView = MapPinCallOut()
        }

        if (self.selected && !calloutViewAdded) {
            addSubview(calloutView!)
        }

        if (!self.selected) {
            calloutView?.removeFromSuperview()
        }
    }

    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        var hitView = super.hitTest(point, withEvent: event)

        if let callout = calloutView {

            if (hitView == nil && self.selected) {
                hitView = callout.hitTest(point, withEvent: event)
            }
        }

        hitOutside = hitView == nil
        return hitView;
    }
}

Here is my UIView : 这是我的UIView

class MapPinCallOut: UIView {
override func hitTest(var point: CGPoint, withEvent event: UIEvent?) -> UIView? {
    let viewPoint = superview?.convertPoint(point, toView: self) ?? point

    let isInsideView = pointInside(viewPoint, withEvent: event)

    var view = super.hitTest(viewPoint, withEvent: event)

    return view
}

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
    return CGRectContainsPoint(bounds, point)
}
}

I have also created a nib file and changed the file's owner to " MapPinCallOut " Then in my viewDidLoad of my viewController , I am adding a pin like this: 我还创建了一个nib文件,并将文件的所有者更改为“ MapPinCallOut ”,然后在viewController viewDidLoad中,添加了一个如下所示的图钉:

let location = CLLocationCoordinate2D(
        latitude: 25.0336,
        longitude: 121.5650
    )
    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegion(center: location, span: span)
    mapView.setRegion(region, animated: true)
    let string = "title"

  let annotation = CustomAnnotation(location: location, title: string, subtitle: string)

    mapView.addAnnotation(annotation)

and then in my viewForAnnotation function: 然后在我的viewForAnnotation函数中:

    var pinView: CustomAnnotationView = CustomAnnotationView()
    pinView.annotation = annotation
    pinView.canShowCallout = true
    return pinView

After all that, I ran my code and when I select the annotation, the callout does appear but it is still the standard annotation. 毕竟,我运行了我的代码,当我选择注释时,标注确实出现了,但它仍然是标准注释。 What am I missing to create my own custom callout view? 创建自己的自定义标注视图时我缺少什么?

Set pinView.canShowCallout to false in your viewForAnnotation . viewForAnnotation pinView.canShowCallout设置为false That may help you out. 那可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM