【发布时间】:2011-11-05 22:09:21
【问题描述】:
我是 iphone 开发新手。我在地图上添加了注释。我能够在 didSelectAnnotationView() 中捕获注释上的点击事件。但是我想在用户点击注释时更改注释的图像。
【问题讨论】:
标签: iphone annotations mapkit
我是 iphone 开发新手。我在地图上添加了注释。我能够在 didSelectAnnotationView() 中捕获注释上的点击事件。但是我想在用户点击注释时更改注释的图像。
【问题讨论】:
标签: iphone annotations mapkit
斯威夫特 3: 我假设您已经在自定义 MKAnnotation 视图中使用此函数
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {...}
我会将 @djibouti33 的答案从 Objective-c 更新为 Swift 3:
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
view.image = UIImage(named: "unselected_marker")
view.frame = CGRect(x: 0, y: 0, width: 40, height: 40)//keep the new icon size resobable.
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
view.image = UIImage(named: "selected_marker")
view.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
}
【讨论】:
如果您使用的是MKAnnotationView 而不是MKPinAnnotationView,您可以只依赖MKMapViewDelegate 方法:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
view.image = [UIImage imageNamed:@"selected_image"];
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
view.image = [UIImage imageNamed:@"deselected_image"];
}
【讨论】:
像这样设置图像属性。
annView.image = [UIImage imageNamed:@"AnnotationIcon.png"];
编辑
所以,你似乎在使用MKPinAnnotationView
它有一个pinColor 属性。
因此,你可以这样改变它
pin.pinColor = MKPinAnnotationColorRed; // green and purple are 2 other colors.
【讨论】: