如何在swift 4中添加虚线?

时间:2022-10-30 14:34:48

i already have a polyline in Swift4, how can i make the style of it to Dashed/Dotted line ??

我已经在Swift4中有了一个折线,如何将它的样式变为虚线/虚线?

  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 个解决方案

#1


1  

You need to implement func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer delegate method and using renderer.lineDashPhase and renderer.lineDashPattern properties you should be able to achive what you need

您需要实现func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay:) -> MKOverlayRenderer委托方法并使用renderer。lineDashPhase和渲染器。lineDashPattern属性你应该能够实现你所需要的

code example

代码示例

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
}

result

结果

如何在swift 4中添加虚线?

#1


1  

You need to implement func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer delegate method and using renderer.lineDashPhase and renderer.lineDashPattern properties you should be able to achive what you need

您需要实现func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay:) -> MKOverlayRenderer委托方法并使用renderer。lineDashPhase和渲染器。lineDashPattern属性你应该能够实现你所需要的

code example

代码示例

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
}

result

结果

如何在swift 4中添加虚线?