未调用 MKMapView MKPointAnnotation 点击事件
MKMapView MKPointAnnotation tap event not called
我正在使用带有 MKPointAnnotation 的 MKMapView(代理设置正确)。注释是在后台线程上调用的这个方法中生成的。
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在这里生成:
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' 正常工作。如您所见,两者都设置正确。
所以我有一个 mapView(委托已设置),'canShowCallOut' 是真实的,标题已设置并且可以正常工作。
要继续 detail-page,我想跟踪 annotationViews ('didSelectAnnotationView') 上的点击,但它没有被调用。
我做错了什么?
好吧,我自己找到了解决方案。
您必须明确设置 annotationViews 框架。如果您只是为视图设置子视图,它们将被显示,但视图框架设置为 0、0(高度、重量)。所以你不能点击它,因为区域也是 0, 0。
我的解决方案是这样的,有趣的是annotationView.frame = CGRect(x: 0, y: 0, width: 67, height: 26)
。其他一切都一样。现在单击注释调用 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
}
我正在使用带有 MKPointAnnotation 的 MKMapView(代理设置正确)。注释是在后台线程上调用的这个方法中生成的。
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在这里生成:
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' 正常工作。如您所见,两者都设置正确。
所以我有一个 mapView(委托已设置),'canShowCallOut' 是真实的,标题已设置并且可以正常工作。
要继续 detail-page,我想跟踪 annotationViews ('didSelectAnnotationView') 上的点击,但它没有被调用。
我做错了什么?
好吧,我自己找到了解决方案。
您必须明确设置 annotationViews 框架。如果您只是为视图设置子视图,它们将被显示,但视图框架设置为 0、0(高度、重量)。所以你不能点击它,因为区域也是 0, 0。
我的解决方案是这样的,有趣的是annotationView.frame = CGRect(x: 0, y: 0, width: 67, height: 26)
。其他一切都一样。现在单击注释调用 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
}