【问题标题】:Custom CalloutView with Swift 2.0 Best Approach?使用 Swift 2.0 最佳方法自定义 CalloutView?
【发布时间】:2015-10-27 09:39:00
【问题描述】:

我想从xib 创建一个自定义CalloutView。我创建了一个 xib 和自定义 AnnotationView 类。我想使用我的MapViewController segue,它的名字是showDetail segue,因为这个 segue 是 Push 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,它执行MapViewController到我的DetailViewControllershowDetail segue。 但问题是它不会从superview 中删除我的customCalloutView,所以我不能关闭标注视图。

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

非常感谢。

【问题讨论】:

    标签: ios swift uiview mkmapview mkannotationview


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      希望对你有帮助

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-10
        • 1970-01-01
        • 2015-12-11
        相关资源
        最近更新 更多