我在我的MKMapView中画了一堆MKPolygons。它们中的一些堆叠在一起。如何将选定的多边形置于顶部/前面?
我在从多边形层创建的MKPolygonView上尝试了bringSubviewToFront::
MKPolygonView *view = (MKPolygonView *)[self.mapView viewForOverlay:polygon];
view.strokeColor = [UIColor orangeColor];
[self.mapView bringSubviewToFront:view];解决方案:
我删除了
MKPolygonView *view = (MKPolygonView *)[self.mapView viewForOverlay:polygon];
view.strokeColor = [UIColor orangeColor];
[self.mapView bringSubviewToFront:view];并将其替换为Craig建议的内容:
[self.mapView insertOverlay:polygon atIndex:self.mapView.overlays.count];然后调用MKMapKit委托mapView:viewForOverlay:,然后我在那里处理颜色更改:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKPolygon class]] && !((MKPolygon *)overlay).isSelected) {
MKPolygonView* aView = [[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay];
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
aView.strokeColor = [UIColor yellowColor];
aView.lineWidth = 3;
return aView;
}
else if ([overlay isKindOfClass:[MKPolygon class]] && ((MKPolygon *)overlay).isSelected) {
MKPolygonView* aView = [[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay];
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
aView.strokeColor = [UIColor orangeColor];
aView.lineWidth = 3;
return aView;
}
}发布于 2013-03-16 17:59:09
将覆盖添加到mapView时,可以选择在覆盖列表中放置该覆盖的位置。由于覆盖图只能在列表中出现一次,因此您只需在所需位置再次插入即可移动它。既然你想把它放在最上面,这应该是可行的:
[mapView insertOverlay:overlay atIndex:[mapView.overlays count]];您不应该调用viewForOverlay。把这个留给iOS吧。如果你需要对覆盖图进行着色,那就在viewForOverlay中进行,因为iOS可以并且将在任何时候调用它,如果你返回一个非彩色的覆盖图,这就是它将绘制的。
https://stackoverflow.com/questions/15420761
复制相似问题