如何使用 Swift 在 mapkit 上更改未选择的注释图钉图像
How to change non selected annotation pin images on mapkit with Swift
我有一张地图,在这张地图上,我有 10 个自定义注释图钉。所有图钉都具有相同的自定义图像。当我点击一个图钉时,我需要更改所有其他 9 个注释的图像。可以更改点击的图钉图像,但我需要保持原样,并且我需要更改所有其他图钉图像。
我尝试使用地图获取所有注释 mapView.annotations
并尝试查找选定的注释并更改其他图像但无法管理。以及如何做的想法?
提前致谢。
遵守MKMapViewDelegate
协议然后:
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
let selectedAnnotation = view.annotation
for annotation in mapView.annotations {
if let annotation = annotation as? MKAnnotation where !annotation.isEqual(selectedAnnotation) {
// do some actions on non-selected annotations in 'annotation' var
}
}
如果您想稍后处理所有注释,您也可以在此处保存所选注释以备后用。
终于解决了 :) 解决了这个问题有点困难,但工作顺利 :) 谢谢你的提示 rshev ;)
我用了一个 bool 来识别点击
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is CustomAnnotation {
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(customAnnotationViewIdentifier)
pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: customAnnotationViewIdentifier)
if tapControl {
pin.image = UIImage(named: "MapAnnotationIcon")
} else {
pin.image = UIImage(named: "SelectedMapAnnotationIcon")
}
if pin == nil {
pin.canShowCallout = false
} else {
pin.annotation = annotation
}
return pin
当引脚被敲击时 ->
if let annotation = view.annotation as? CustomAnnotation {
tapControl = !tapControl
for annotation in mapView.annotations {
if let annotation = annotation as? MKAnnotation where !annotation.isEqual(selectedAnnotation) {
mapView.removeAnnotation(annotation)
}
}
addAnnotations()
println("tapped")
我删除了所有没有选定引脚的引脚,然后将它们拉回来,但这次 tapcontrol 是假的,所以其他引脚用另一个 imageview 重新绘制,这就是我真正想要做的。
您只需在 MKAnnotationView
子类中覆盖 isSelected
属性。
override var isSelected: Bool {
didSet {
if isSelected {
// do stuff where annotation is selected
} else {
// do opposite
}
}
}
我有一张地图,在这张地图上,我有 10 个自定义注释图钉。所有图钉都具有相同的自定义图像。当我点击一个图钉时,我需要更改所有其他 9 个注释的图像。可以更改点击的图钉图像,但我需要保持原样,并且我需要更改所有其他图钉图像。
我尝试使用地图获取所有注释 mapView.annotations
并尝试查找选定的注释并更改其他图像但无法管理。以及如何做的想法?
提前致谢。
遵守MKMapViewDelegate
协议然后:
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
let selectedAnnotation = view.annotation
for annotation in mapView.annotations {
if let annotation = annotation as? MKAnnotation where !annotation.isEqual(selectedAnnotation) {
// do some actions on non-selected annotations in 'annotation' var
}
}
如果您想稍后处理所有注释,您也可以在此处保存所选注释以备后用。
终于解决了 :) 解决了这个问题有点困难,但工作顺利 :) 谢谢你的提示 rshev ;)
我用了一个 bool 来识别点击
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is CustomAnnotation {
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(customAnnotationViewIdentifier)
pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: customAnnotationViewIdentifier)
if tapControl {
pin.image = UIImage(named: "MapAnnotationIcon")
} else {
pin.image = UIImage(named: "SelectedMapAnnotationIcon")
}
if pin == nil {
pin.canShowCallout = false
} else {
pin.annotation = annotation
}
return pin
当引脚被敲击时 ->
if let annotation = view.annotation as? CustomAnnotation {
tapControl = !tapControl
for annotation in mapView.annotations {
if let annotation = annotation as? MKAnnotation where !annotation.isEqual(selectedAnnotation) {
mapView.removeAnnotation(annotation)
}
}
addAnnotations()
println("tapped")
我删除了所有没有选定引脚的引脚,然后将它们拉回来,但这次 tapcontrol 是假的,所以其他引脚用另一个 imageview 重新绘制,这就是我真正想要做的。
您只需在 MKAnnotationView
子类中覆盖 isSelected
属性。
override var isSelected: Bool {
didSet {
if isSelected {
// do stuff where annotation is selected
} else {
// do opposite
}
}
}