自定义CalloutView与Swift 2.0最佳方法?

时间:2015-10-27 09:39:00

标签: ios swift uiview mkmapview mkannotationview

我想从CalloutView创建自定义xib。我创建了一个xib和自定义AnnotationView类。我想使用名为MapViewController segue的showDetail segue,因为此segue是推送 segue。当用户点击我的calloutView中的按钮时,它应该执行我的showDetail segue。

我搜索了所有文档,教程,指南和问题,但我可以 找不到 Swift 的任何解决方案。也没有自定义库。我在3天内找不到任何解决办法。

我的CalloutView.xib文件View是我的CustomAnnotationView,其名称为CalloutAnnotationView.swift文件所有者MapViewController,它允许我使用MapViewController segues。

有我的CalloutAnnotationView.swift

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        let hitView = super.hitTest(point, withEvent: event)
        if (hitView != nil) {
            self.superview?.bringSubviewToFront(self)
        }
        return hitView
    }

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        let rect = self.bounds
        var isInside = CGRectContainsPoint(rect, point)
        if (!isInside) {
            for view in self.subviews {
                isInside = CGRectContainsPoint(view.frame, point)
                if (isInside) {

                }
            }
        }
        return isInside
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        let calloutViewController = MapViewController(nibName: "CalloutView", bundle: nil)

        if selected {

            calloutViewController.view.clipsToBounds = true
            calloutViewController.view.layer.cornerRadius = 10
            calloutViewController.view.center = CGPointMake(self.bounds.size.width * 0.5, -calloutViewController.view.bounds.size.height * 0.5)
            self.addSubview(calloutViewController.view)


        } else {
            // Dismiss View
            calloutViewController.view.removeFromSuperview()
        }
    }

我的MapViewController.swift代码;

// MARK: - MapKit Delegate Methods

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if let annotation = annotation as? Veterinary { // Checking annotations is not User Location.
            var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(Constants.MapViewAnnotationViewReuseIdentifier) as? CalloutAnnotationView
            if pinView == nil { // If it is nil it created new Pin View
                pinView = CalloutAnnotationView(annotation: annotation, reuseIdentifier: Constants.MapViewAnnotationViewReuseIdentifier)
                pinView?.canShowCallout = false
            } else { pinView?.annotation = annotation } // If it is NOT nil it uses old annotations.
            // Checking pin colors from Veterinary Class PinColor Method
            pinView?.image = annotation.pinColors()
            //pinView?.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)

            return pinView
        }
        return nil
    }

这些代码可以正常加载我的xib文件。我从按钮创建了IBAction,并且执行MapViewControllerDetailViewController的{​​{1}} segue。 但问题是它没有从showDetail删除我的 customCalloutView ,因此我无法关闭标注视图。

如何解决此问题或创建自定义superview以使用calloutView推送segues的最佳方法是什么?

非常感谢。

2 个答案:

答案 0 :(得分:0)

在您的MKMapView委托中查看是否在segueing时调用它:

func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
   if let annotation = view.annotation as? Veterinary {
        mapView.removeAnnotation(annotation)
    }
}

答案 1 :(得分:0)

我希望这可以帮助您解决问题

func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
    if view.isKindOfClass(AnnotationView)
    {
        for subview in view.subviews
        {
            subview.removeFromSuperview()
        }
    }
}