Turn NSGradient into NSColor in Swift

92 Views Asked by At

I am trying to make an MKPolyline for a SwiftUI map where it shows a persons location for a day and I want a gradient changing from blue to green from the first point in their location to the last point in blue. I have this code

renderer.strokeColor = NSGradient(colors: [NSColor.blue, NSColor.green])

I have also tried

renderer.strokeColor = NSColor(NSGradient(colors: [NSColor.blue, NSColor.green]))

and

renderer.strokeColor = NSColor(Color(Gradient(colors: [Color.blue, Color.green])))

but these all return errors about turning Gradients into colors. Thanks!

1

There are 1 best solutions below

0
On

Thanks to @vadian I did this

if let routePolyline = overlay as? MKPolyline {
  let renderer = MKGradientPolylineRenderer(polyline: routePolyline)
    renderer.setColors([NSColor.blue, NSColor.green], locations: [])
  renderer.lineWidth = 2
  return renderer
}