作者:艺卓显示、巴可投影 | 来源:互联网 | 2023-01-20 14:08
我正在尝试子类化MKPolyline
并MKGeodesicPolyline
存储他们自己的颜色(通过让子类实例返回自己的颜色MKPolylineRenderer
).它工作正常MKPolyline
,但我的MKGeodesicPolyline
子类的实例不是子类 - 只是MKGeodesicPolyline
s.有谁能解释为什么?这是我的代码......
protocol MapLineProtocol: MKOverlay {
var width: CGFloat { get set }
var colour: UIColor { get set }
}
extension MapLineProtocol {
var renderer: MKPolylineRenderer {
let polylineRenderer = MKPolylineRenderer(overlay: self)
polylineRenderer.strokeColor = self.colour
polylineRenderer.lineWidth = self.width
return polylineRenderer
}
}
class MapLine: MKPolyline, MapLineProtocol {
var width: CGFloat = 3
var colour: UIColor = .blue
convenience init(start: CLLocationCoordinate2D, end: CLLocationCoordinate2D) {
let line = [start, end]
self.init(coordinates: line, count: line.count)
}
}
class MapGeodesic: MKGeodesicPolyline, MapLineProtocol {
var width: CGFloat = 3
var colour: UIColor = .red
convenience init(start: CLLocationCoordinate2D, end: CLLocationCoordinate2D) {
let line = [start, end]
self.init(coordinates: line, count: line.count)
}
}
let mapLine = MapLine(start: loc.coordinate, end: end)
print("Mapline subclass: \(mapLine)") //
self.mapView.add(mapLine)
let geoLine = MapGeodesic(start: loc.coordinate, end: end)
print("Geodesic subclass: \(geoLine)") // !!!
self.mapView.add(geoLine)
访问.colour
属性mapLine
很好(并且渲染器可以工作),但访问.colour
geoLine会导致运行时异常(当然,如果绕过颜色,渲染器也不起作用).有人可以解释一下吗?