ios - 使用 Swift 2.0 自定义 CalloutView 最佳方法?

标签 ios swift uiview mkmapview mkannotationview

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

我搜索了所有文档、教程、指南和问题,但我可以 没有找到任何Swift的解决方案。也没有自定义库。我已经三天没有找到任何解决方案了。

我的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,它执行MapViewController到我的DetailViewControllershowDetail 继续。 但问题是它不会从superview中删除我的customCalloutView,因此我无法忽略标注 View 。

我该如何解决这个问题或者创建自定义calloutView以使用我的MapViewController推送segues的最佳方法是什么?

非常感谢。

最佳答案

在您的 MKMapView 委托(delegate)中查看在继续时是否调用此函数:

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

关于ios - 使用 Swift 2.0 自定义 CalloutView 最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33364373/

相关文章:

ios - 如何使用我的应用程序中使用的自定义颜色轻松支持浅色和深色模式?

ios - 如果您有 2 个不同的搜索栏用于 2 个不同的索引,如何配置 Algolia InstantSearch 类?

ios - Swift 搜索栏( Controller )内存泄漏

iphone - UIButton 没有响应

iphone - 将 View 带回其他 iPhone

iphone - Bundle ID 和 SKU 能否在另一个 iTunes Connect 帐户中重复使用?

ios - 尝试从核心数据中获取数据时应用程序崩溃

ios - 点击按钮后如何向添加的新 subview 添加约束

swift - Firebase 相当于 .com/nextLevel 的新版本是什么

ios - 界面生成器 : What are the UIView's Layout iOS 6/7 Deltas for?