🦋
SwiftUI: Pathで二色の破線を描く

白黒二色の交互の破線を描きます。
ZStack {
Path(CGRect(x: 10, y: 10, width: 200, height: 200))
.strokedPath(StrokeStyle(lineWidth: 2.0,
dash: [10.0, 30.0]))
.foregroundColor(Color.white)
Path(CGRect(x: 10, y: 10, width: 200, height: 200))
.strokedPath(StrokeStyle(lineWidth: 2.0,
dash: [10.0, 30.0],
dashPhase: 20.0))
.foregroundColor(Color.black)
}
StrokeStyleのdashPhaseを上手に使うことでできます。
-
dashの配列には[破線, 空白, 破線, 空白, …]のように破線から始まる長さを入れていきます。 -
dashPhaseには破線が始まるまでのオフセットを指定できます。
| 上の層(黒) | - | - | 黒 | - | - | - | 黒 | - | - | - | ... |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 下の層(白) | 白 | - | - | - | 白 | - | - | - | 白 | - | ... |
Discussion