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

问题描述 投票:0回答:1

在 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)
}

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

创建这样的东西 -

笔划样式1(全线)从0到

n

笔画样式 2(虚线)从

n
到 1。

其中

n
可以是任何浮点数
0<=n<=1

ios swift swiftui shapes swiftui-environment
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.