在同一路径上创建不同的描边样式 - SwiftUI(基于 0-1 之间的值,如渐变)

Kun*_*rma 6 shapes ios swift swiftui swiftui-environment

在 SwiftUI Shapes 中,我们可以使用渐变来制作不同的颜色描边。

例如-

@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)
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

是否可以通过任何方式对笔划样式执行相同的操作?

创建这样的东西 -

在此输入图像描述

笔划样式 1(实线)从 0 到n,

描边样式 2(虚线)从n到 1。

哪里n可以是任意浮点数0<=n<=1

小智 0

我认为你只能使用渐变来实现这一点