未调用MKMapView MKPointAnnotation tap事件 [英] MKMapView MKPointAnnotation tap event not called

查看:97
本文介绍了未调用MKMapView MKPointAnnotation tap事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有MKPointAnnotation的MKMapView(正确设置委托)。在后台线程上调用此方法生成注释。

I am using a MKMapView (delegate is set correctly) with a MKPointAnnotation. The annotations are generated in this method called on a background thread.

func updateMapAnnotations() {
    for var i = 0; i < DataManager.getStationList().count; i++ {
        var s = DataManager.getStationList()[i] as Station
        var annotation = MKPointAnnotation()
        annotation.setCoordinate(CLLocationCoordinate2D(latitude: s.latitude, longitude: s.longitude))
        annotation.title = "\(s.id)"
        dispatch_async(dispatch_get_main_queue(), {
            self.mapView.addAnnotation(annotation)
        })
    }
}

annotationViews在这里生成:

The annotationViews are generated in here:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        return nil
    }

    let reuseId = "StationAnnotation"

    let annoStation = DataManager.getStationById(annotation.title!.toInt()!)

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {

        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        let base = UIView(frame: CGRect(x: 0, y: 0, width: 67, height: 26))

        let imageView = UIImageView(frame: CGRect(x: 2, y: 2, width: 22, height: 22))
        imageView.image = UIImage(named: "test")

        base.layer.cornerRadius = 3.0
        base.clipsToBounds = true
        base.backgroundColor = UIColor.whiteColor()

        var priceLabelBig = UILabel(frame: CGRect(x: 25, y: 0, width: 30, height: 25))
        priceLabelBig.textColor = UIColor.blackColor()
        priceLabelBig.font = UIFont(name: priceLabelBig.font.fontName, size: 15)

        var priceLabelSmall = UILabel(frame: CGRect(x: 55, y: 0, width: 12, height: 15))
        priceLabelSmall.textColor = UIColor.blackColor()
        priceLabelSmall.font = UIFont(name: priceLabelBig.font.fontName, size: 12)

        if let curPrice = annoStation?.getTextWithSettings().description {
        var stringLength = countElements(curPrice)
        var substringToIndex = stringLength - 1
            priceLabelBig.text = curPrice.substringToIndex(advance(curPrice.startIndex, substringToIndex))
            priceLabelSmall.text = curPrice.substringFromIndex(advance(curPrice.startIndex, substringToIndex))
        }

        base.addSubview(imageView)
        base.addSubview(priceLabelBig)
        base.addSubview(priceLabelSmall)

        anView.addSubview(base)
        anView.canShowCallout = true
    }
    else {
        anView.annotation = annotation
    }

    return anView
}

我知道我必须设置注释标题并将'canShowCallOut'设置为true以使'didSelectAnnotationView'正常工作。正如你所看到的,两者都设置正确。

I know I have to set the annotations title and set 'canShowCallOut' to true to get the 'didSelectAnnotationView' working. As you can see, both is set correctly.

所以我所拥有的是mapView(委托设置),'canShowCallOut'为真,标题设置和工作。

So what I have is a mapView (delegate is set), 'canShowCallOut' is true and a title is set and working.

要进入详情页面,我想跟踪anotationViews('didSelectAnnotationView')的点击,但是没有调用它。

To go on a detail-page, I want to track a tap on the annotationViews ('didSelectAnnotationView'), but it is not called.

我做错了什么?

推荐答案

好的我找到了我自己解决。

Okay I found the solution by myself.

你必须明确设置annotationViews框架。如果您只为视图设置子视图,它们将被显示,但视图框架设置为0,0(高度,重量)。所以你就是无法点击它,因为该区域也是0,0。

You have to set the annotationViews frame explicitly. If you just set subViews for the view, they will be shown, but the views frame is set to 0, 0 (height, weight). So you just can't tap it, cause the area is 0, 0 as well.

我的解决方案就是这个,有趣的一行是 annotationView .frame = CGRect(x:0,y:0,宽度:67,高度:26)。其他一切都是一样的。现在单击注释会调用 didSelectAnnotationView

My solution is this, the interesting line is annotationView.frame = CGRect(x: 0, y: 0, width: 67, height: 26). Everything else is the same. Now a click on a annotation calls didSelectAnnotationView.

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        return nil
    }

    let reuseId = "stationAnnotation"

    let annoStation = DataManager.getStationById(annotation.title!.toInt()!)

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if annotationView == nil {

        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        annotationView.frame = CGRect(x: 0, y: 0, width: 67, height: 26)
        let base = UIView(frame: CGRect(x: 0, y: 0, width: 67, height: 26))
        base.userInteractionEnabled = true

        let imageView = UIImageView(frame: CGRect(x: 2, y: 2, width: 22, height: 22))
        imageView.image = UIImage(named: "zapfsaeule")

        base.layer.cornerRadius = 3.0
        base.clipsToBounds = true
        base.backgroundColor = UIColor.whiteColor()

        var priceLabelBig = UILabel(frame: CGRect(x: 25, y: 0, width: 30, height: 25))
        priceLabelBig.textColor = UIColor.blackColor()
        priceLabelBig.font = UIFont(name: priceLabelBig.font.fontName, size: 15)

        var priceLabelSmall = UILabel(frame: CGRect(x: 55, y: 0, width: 12, height: 15))
        priceLabelSmall.textColor = UIColor.blackColor()
        priceLabelSmall.font = UIFont(name: priceLabelBig.font.fontName, size: 12)

        if let curPrice = annoStation?.getPriceWithSettings().description {
            var stringLength = countElements(curPrice)
            var substringToIndex = stringLength - 1
            priceLabelBig.text = curPrice.substringToIndex(advance(curPrice.startIndex, substringToIndex))
            priceLabelSmall.text = curPrice.substringFromIndex(advance(curPrice.startIndex, substringToIndex))
        }

        base.addSubview(imageView)
        base.addSubview(priceLabelBig)
        base.addSubview(priceLabelSmall)

        annotationView.addSubview(base)
        annotationView.annotation = annotation
    }
    else {
        annotationView.annotation = annotation
    }

    return annotationView
}

这篇关于未调用MKMapView MKPointAnnotation tap事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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