首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义CalloutView与SWIFT2.0最佳方法?

自定义CalloutView与SWIFT2.0最佳方法?
EN

Stack Overflow用户
提问于 2015-10-27 09:39:00
回答 2查看 597关注 0票数 0

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

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

我的CalloutView.xib文件View是My CustomAnnotationView,它的名字是CalloutAnnotationView.swift文件的所有者MapViewController,它允许我使用MapViewController

这是我的CalloutAnnotationView.swift

代码语言:javascript
复制
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码;

代码语言:javascript
复制
// 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文件。我从一个按钮创建了,用showDetail执行从MapViewController到my DetailViewController的搜索。但问题是,customCalloutView不能从superview中删除,所以不能拒绝标注视图。

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

非常感谢

EN

回答 2

Stack Overflow用户

发布于 2016-04-08 23:30:01

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

代码语言:javascript
复制
func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
   if let annotation = view.annotation as? Veterinary {
        mapView.removeAnnotation(annotation)
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-04-13 07:17:11

我希望这能帮你解决问题

代码语言:javascript
复制
func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
    if view.isKindOfClass(AnnotationView)
    {
        for subview in view.subviews
        {
            subview.removeFromSuperview()
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33364373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档