【问题标题】:Customizing callout view for MKAnnotation in Swift在 Swift 中为 MKAnnotation 自定义标注视图
【发布时间】:2015-05-08 16:01:43
【问题描述】:

我需要一个完全可自定义的 MKAnnotation 标注。我将MKAnnotationViewUIView 子类化。我的代码:

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;
    }
}

这是我的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)
}
}

我还创建了一个 nib 文件并将文件的所有者更改为“MapPinCallOut” 然后在我的viewControllerviewDidLoad 中,我添加了这样的图钉:

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)

然后在我的viewForAnnotation函数中:

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

毕竟,我运行了我的代码,当我选择注释时,标注确实出现了,但它仍然是标准注释。创建自己的自定义标注视图时我缺少什么?

【问题讨论】:

  • 你有一个调试器。调试!浏览setSelected 代码。它在运行吗?它是否符合您的预期?

标签: ios swift mkannotation mkannotationview


【解决方案1】:

在您的viewForAnnotation 中将pinView.canShowCallout 设置为false。这可能会对你有所帮助。

【讨论】:

    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多