问题描述
我想创建地图应用。例如,日本的不同地区。它们都是不同的形状,而不是正方形。你推它,它会改变颜色。是否可以在Xcode中创建异常形式的按钮(地图上区域的形式)?
解决方法
绘图
为了在地图上绘制自定义形状,请使用MKPolygon
的自定义子类,在其中定义所选的状态颜色和selected
标志。
internal final class MyPolygon: MKPolygon {
internal var selected = false
internal var selectedColor = UIColor.red.withAlphaComponent(0.6)
...
}
为了为自定义MKPolygon
绘制自定义颜色,请创建MKPolygonRenderer
的子类,并在selectedColor
具有{{1} }。
实现这两个方法后,将叠加层添加到selected == true
MKMapView
将mapView.addOverlay(polygon)
分配给delegate
,然后从委托人的方法返回多边形的渲染器:
mapView
捕获用户输入
将func mapView(_ mapView: MKMapView,rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let polygon = overlay as? MyPolygon {
return MyPolygonRenderer(polygon: polygon)
}
return MKOverlayRenderer(overlay: overlay)
}
添加到您的UITapGestureRecognizer
中:
mapView