โจeven-odd fill mode
Last updated
Last updated
โฌ๏ธ ้่ฆ๏ผ
import SwiftUI
import Vector2D // for ๐Rectangular
struct EOFillShape: Shape {
// โญ ่ฆ็ซๅนพๅฑค็็ท
let layers: Int
func path(in rect: CGRect) -> Path {
let dx = pad(rect) // ่จ็ฎๅ
ง็ธฎ็้
return Path { path in
for i in 0..<layers {
// ๐CGRect + .inset(by:), ๐Int+
path.addRect(rect.inset(by: dx * i))
}
}
}
}
extension EOFillShape {
// ่จ็ฎๆฏๆฌกๅ
ง็ธฎ (inset) ็้
func pad(_ rect: CGRect) -> CGFloat {
// ๐Rectangular, ๐Int+
let padding = rect.minSide / 2.0 / layers
return min(20, padding) // ไธ่ถ
้ 20
}
}
struct EOFillShape_Previews: PreviewProvider {
static var previews: some View {
HStack {
ForEach(1..<6){ i in
let shape = EOFillShape(layers: i)
let black = Color.black
VStack {
shape
// โญ use even-odd fill mode
// ๐ShapeStyle+ , FillStyle+
.fill(.diagonal(.green, .blue), style: .evenOddFill)
// โญ ๅฆๆๆฒๆ compositingGroup๏ผๆดๅก้ฝๆๆ้ฐๅฝฑโ
.compositingGroup()
.shadow(radius: 8)
.overlay(shape.stroke(black))
shape
// ๐ShapeStyle+
.fill(.diagonal2(.pink, .purple, .blue))
.shadow(radius: 8)
.overlay(shape.stroke(black))
}
}
}
.frame(height: 200)
.padding()
.background(Color.gray)
}
}
SwiftOnTap โฉ View โฉ compositingGroup() โ some View.
ไฝฟ็จ compositing group ๆไธๆ่ฎๆ็ฉบ็้จๅไนๆ้ฐๅฝฑใ
.destinationOut ๆทท่ฒๆจกๅผไนๅฏไปฅ็จไพๆๆดใ
How can I clip a Shape with another Shape in SwiftUI? โญ๏ธ - even-odd fill mode.