在 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
我认为你可以只使用渐变来实现这一点