如何在swift 4中添加虚线折线?

时间:2018-03-18 11:11:40

标签: ios swift swift3 xcode8 xcode9

我已经在Swift4中有一条折线,我怎么能把它的风格改成Dashed / Dotted线?

  let   path = GMSMutablePath(path: GMSPath())
    for marker in markerList {
        bounds = bounds.includingCoordinate(marker.position)
        path.add(marker.position)

    }



    let polyline = GMSPolyline(path: path)
    polyline.strokeColor = UIColor(named: "Primary")!
    polyline.strokeWidth = 3.0
    polyline.geodesic = true
    polyline.map = mapView

Like this map

1 个答案:

答案 0 :(得分:1)

您需要实施func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer委托方法并使用renderer.lineDashPhaserenderer.lineDashPattern属性,您应该能够获得所需内容

代码示例

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.red
    renderer.lineWidth = 4.0
    renderer.lineDashPhase = 2
    renderer.lineDashPattern = [NSNumber(value: 1),NSNumber(value:5)]
    return renderer
}

<强>结果

enter image description here