如何在Swift中制作图钉注释标注? [英] How do I make a pin annotation callout in Swift?

查看:96
本文介绍了如何在Swift中制作图钉注释标注?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使标注正常工作,但没有发生,因为我在准备序号时做错了什么.我想知道如何对另一个视图进行图钉注释标注吗?

I tried to make the callout work but that didn't happen as I did something wrong in my prepare for segue. I want to know how to be able to make a pin annotation callout to another view?

推荐答案

点击标注中的按钮时,切换到另一个场景的过程如下:

The process of segueing to another scene when the button in the callout is tapped is like so:

  1. 将地图视图的delegate设置为视图控制器.您可以在Interface Builder的连接检查器"中或以编程方式执行此操作.您还希望指定视图控制器也符合MKMapViewDelegate.

  1. Set the delegate of the map view to be the view controller. You can do this either in Interface Builder's "Connections Inspector" or programmatically. You want to specify that the view controller conforms to MKMapViewDelegate, too.

创建注释时,请确保也设置标题:

When you create the annotation, make sure to set the title, too:

let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = ...
mapView.addAnnotation(annotation)

  • 使用按钮定义带有标注的注释视图子类:

  • Define an annotation view subclass with callout with a button:

    class CustomAnnotationView: MKPinAnnotationView {  // or nowadays, you might use MKMarkerAnnotationView
        override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
            super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
    
            canShowCallout = true
            rightCalloutAccessoryView = UIButton(type: .infoLight)
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    }
    

  • 指示您的MKMapView使用此注释视图. iOS 11简化了该过程,但我将介绍两种方法的实现方式:

  • Instruct your MKMapView to use this annotation view. iOS 11 has simplified that process, but I’ll describe how to do it both ways:

    • 如果您的最低iOS版本为11(或更高),则只需将自定义注释视图注册为默认视图即可.通常,您在iOS 11及更高版本中根本不实现mapView(_:viewFor:). (唯一可能实现该方法的方法是,因为您有多种类型的自定义注释类型,因此需要注册多个重用标识符.)

    • If your minimum iOS version is 11 (or later), you’d just register the custom annotation view in as the default and you’re done. You generally don't implement mapView(_:viewFor:) at all in iOS 11 and later. (The only time you might implement that method is if you needed to register multiple reuse identifiers because you had multiple types of custom annotation types.)

    override func viewDidLoad() {
        super.viewDidLoad()
    
        mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
    }
    

  • 如果您需要支持11之前的iOS版本,请确保将视图控制器指定为MKMapView的委托,然后实施mapView(_:viewFor:):

  • If you need to support iOS versions prior to 11, you would make sure to specify your view controller as the delegate for the MKMapView and then would implement mapView(_:viewFor:):

    extension ViewController: MKMapViewDelegate {
        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            if annotation is MKUserLocation { return nil }
    
            let reuseIdentifier = "..."
    
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
    
            if annotationView == nil {
                annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
            } else {
                annotationView?.annotation = annotation
            }
    
            return annotationView
        }
    }
    

  • 例如,使用右侧的.infoLight按钮产生一个如下所示的标注:

    For example, that yields a callout something that looks like the following, with the .infoLight button on the right:

    以编程方式执行segue的实施calloutAccessoryControlTapped:

    Implement calloutAccessoryControlTapped that programmatically performs the segue:

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        performSegue(withIdentifier: "SegueToSecondViewController", sender: view)
    }
    

    很显然,这假设您已经在两个视图控制器之间定义了一个顺序.

    Obviously, this assumes that you've defined a segue between the two view controllers.

    搜索时,将必要的信息传递到目标场景.例如,您可以传递对注释的引用:

    When you segue, pass the necessary information to the destination scene. For example, you might pass a reference to the annotation:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? SecondViewController,
            let annotationView = sender as? MKPinAnnotationView {
            destination.annotation = annotationView.annotation as? MKPointAnnotation
        }
    }
    

    有关更多信息,请参见

    For more information, see Creating Callouts in the Location and Maps Programming Guide.

    对于上述的Swift 2实现,请参见此答案的先前版本.

    For Swift 2 implementation of the above, see previous revision of this answer.

    这篇关于如何在Swift中制作图钉注释标注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆