Swift - 自定义MKAnnotationView,设置标签标题 [英] Swift - Custom MKAnnotationView, set label title

查看:109
本文介绍了Swift - 自定义MKAnnotationView,设置标签标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的mapView标注气泡自定义MKAnnotationView。我可以在创建注释时设置注释标题,也可以自定义MKAnnotationView以添加标签或图像等(在 viewForAnnotation 委托中)但是如何更改在 viewForAnnotation 委托中创建的标签,以便每个引脚的标题不同?

I am trying to customise the MKAnnotationView for my mapView callout bubbles. I am fine with setting the annotation title when the annotation is created, and also customising the MKAnnotationView to add labels or images e.t.c (in the viewForAnnotation delegate), but how do I change the label created in the viewForAnnotation delegate, so that the title of it is different for each pin?

我遇到的另一个问题是,如果我在viewDidLoad方法中创建注释时没有为注释添加标题或副标题,但我仍然尝试创建一,留下 self.map.addAnnotation(注释),当我运行应用程序并点击图钉时,不显示任何标注气泡。

The other issue I have is that if I don't add a title or subtitle to the annotation when it is created in the viewDidLoad method, but I still try and create one by leaving self.map.addAnnotation(annotation), when I run the app and tap the pin no callout bubble is displayed.

最后我想要完全定制的标注气泡,每个针脚都有单独的标签。所以我真正需要知道的是,在创建注释时如何访问viewForAnnotation委托以更改每个引脚的属性。

In the end I would like to have totally customised callout bubbles, with individual labels on them for each pin. So what i really ned to know is how to access the viewForAnnotation delegate when the annotation is created to change properties of it for each pin.

override func viewDidLoad() {
        super.viewDidLoad()

        var countries: [String] = ["Germany","Germany","Poland","Denmark"]
        var practiceRoute: [CLLocationCoordinate2D] = [CLLocationCoordinate2DMake(50, 10),CLLocationCoordinate2DMake(52, 9),CLLocationCoordinate2DMake(53, 20),CLLocationCoordinate2DMake(56, 14)]

        for vari=0; i<practiceRoute.count; i++ {

            var annotation = MKPointAnnotation
            annotation.title = countries[i]
            annotation.coordinate = practiceRoute[i]
            self.map.addAnnotation(annotation)

        }

}

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

    if(pinView==nil){

        pinView=MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true

        let base = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
        base.backgroundColor = UIColor.lightGrayColor()

        let label1 = UILabel(frame: CGRect(x: 30, y: 10, width: 60, height: 15))
        label1.textColor = UIColor.blackColor()
        label1.text = "12 photos"
        base.addSubview(label1)
        pinView!.leftCalloutAccessoryView = base
        pinView!.pinColor = .Red

    }

    return pinView

}


推荐答案

制作自定义注释视图



没有公共API允许您直接访问弹出窗口中的标签。你需要做的是创建一个 MKPinAnnotationView 的子类,并做你想要的任何自定义。例如,

Make your custom annotation view

There is no public API allowing you to access the label in the pop up directly. What you need to do is make a subclass of MKPinAnnotationView and do whatever customization you want there. As an example,

class CustomAnnotationView : MKPinAnnotationView
{
    let selectedLabel:UILabel = UILabel.init(frame:CGRectMake(0, 0, 140, 38))

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

        if(selected)
        {
            // Do customization, for example:
            selectedLabel.text = "Hello World!!"
            selectedLabel.textAlignment = .Center
            selectedLabel.font = UIFont.init(name: "HelveticaBold", size: 15)
            selectedLabel.backgroundColor = UIColor.lightGrayColor()
            selectedLabel.layer.borderColor = UIColor.darkGrayColor().CGColor
            selectedLabel.layer.borderWidth = 2
            selectedLabel.layer.cornerRadius = 5
            selectedLabel.layer.masksToBounds = true

            selectedLabel.center.x = 0.5 * self.frame.size.width;
            selectedLabel.center.y = -0.5 * selectedLabel.frame.height;
            self.addSubview(selectedLabel)
        }
        else
        {
            selectedLabel.removeFromSuperview()
        }
    }
}    


  1. 在地图视图中使用此自定义视图:

  1. Use this custom view in the map view:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    var anno = mapView.dequeueReusableAnnotationViewWithIdentifier("Anno")
    if anno == nil
    {
        anno = CustomAnnotationView.init(annotation: annotation, reuseIdentifier: "Anno")
    }
    return anno;
}


  • 由于标题属性,您必须自己调用地图视图函数 selectAnnotation 。将以下内容添加到 CustomAnnotationView 类:

  • Since the title property of the annotation is not set, you will have to call the map view function selectAnnotation yourself. Add the following to the CustomAnnotationView class:

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        mapView?.selectAnnotation(self.annotation!, animated: true)
    }
    


  • 如果你想在地图上有多个标记:

    If you want to have more than one marker on the map:

    通常只是在初始化期间简单地绘制注释。在setSelected中只返回false(意思是始终显示所有注释)。

    Usually just draw the annotation simply during initialization. In setSelected just return false (meaning "show all annotations all the time").

    class DotAnnotationView : MKPinAnnotationView {
    
        let dot: UILabel = UILabel.init(frame:CGRect(x: 0, y: 0, width: 20, height: 20))
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            _setup()
        }
    
        override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
            super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
            _setup()
        }
    
        override func prepareForReuse() {
            dot.text = "you forgot to set the text value?"
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(false, animated: animated)
        }
    
        func _setup() {
            dot.textAlignment = .center
            .. etc
        }
    
    }
    

    您可以在mapView#viewFor中为每个注释设置字符串(或其他值 - 例如面板的颜色)。这就像在UITableView中填充单元格一样。

    You set the string (or other values - say color of the panel) for each annotation in mapView#viewFor. It's like populating a cell in a UITableView.

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
        let textForThisItem = annotation.title!!
        // or, just use index#of to determine which row this is in your data array
        if annotation.isEqual(mkMap.userLocation) {
            // skip the user-position indicator
            return nil
        }
    
        var anno = mapView.dequeueReusableAnnotationView(withIdentifier: "anno")
    
        if anno == nil {
            anno = DotAnnotationView.init(annotation: annotation, reuseIdentifier: "anno")
        }
    
        (anno as! DotAnnotationView).dot.text = textForThisItem
    
        return anno
    }
    

    最后请注意,如果您只是简单地更改了CustomAnnotationView的类,那就有些令人困惑MKPinAnnotationView到MKAnnotationView,一切都是一样的,但它取代了所有的pin而不仅仅是注释。

    Finally note that somewhat confusingly, if you very simply change the class of CustomAnnotationView from MKPinAnnotationView to MKAnnotationView, everything works the same but it replaces "all of the pin" rather than just the annotation.

    这篇关于Swift - 自定义MKAnnotationView,设置标签标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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