5

In SwiftUI Shapes we can make different color strokes by using gradients.

Eg -

@ViewBuilder
func lineWithSecondColorStyleFromPositionN() -> some View {
    let n = 0.5
    GeometryReader { gr in
        Path { path in
            path.move(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: gr.size.width, y: gr.size.height))
        }
        .stroke(
            LinearGradient(stops: [
                Gradient.Stop(color: .red, location: 0),
                Gradient.Stop(color: .red, location: n),
                Gradient.Stop(color: .blue, location: n),
                Gradient.Stop(color: .blue, location: 1)
            ], startPoint: .top, endPoint: .bottom),
            style: StrokeStyle(lineWidth: 10, lineCap: .butt)
        )
    }
    .frame(height: 200)
}

enter image description here

Is it possible by any means to do the same for stroke styles?

To create something like this -

enter image description here

Stroke Style 1(Full Line) from 0 to n,

Stroke Style 2(Dashed) from n to 1.

Where n can be any floating number 0<=n<=1

Kunal Verma
  • 562
  • 4
  • 15

1 Answers1

0

You can use only Gradient to achieve this I think

starball
  • 20,030
  • 7
  • 43
  • 238