我想在形状的描边上应用内阴影。我找到了一个处理内阴影的函数(无法使用 iOS 16),但我很难将此函数应用于描边本身。
以下是我的代码:
类似于这个: 我尝试在颜色本身上应用内阴影,但由于它不是一个形状,所以无法编译:
以下是我的代码:
struct ContentView: View {
var body: some View {
VStack {
PolygonShape(sides: 7)
.stroke(Color.white, lineWidth: 10)
PolygonShape(sides: 7)
.innerShadow(color: .red, radius: 10)
}
.padding(32)
.background(Color.black.ignoresSafeArea())
}
}
extension Shape {
func innerShadow(color: Color, radius: Double) -> some View {
overlay(
stroke(color, lineWidth: radius)
.blur(radius: radius)
.mask(self)
)
}
}
struct PolygonShape: Shape {
let sides: Int
func path(in rect: CGRect) -> Path {
let c = CGPoint(x: rect.width/2.0, y: rect.height/2.0)
let r = Double(min(rect.width,rect.height)) / 2.0
var vertices:[CGPoint] = []
for i in 0...sides {
let angle = (2.0 * Double.pi * Double(i)/Double(sides))
let pt = CGPoint(x: r * cos(angle), y: r * sin(angle))
vertices.append(CGPoint(x: pt.x + c.x, y: pt.y + c.y))
}
var path = Path()
for (n, pt) in vertices.enumerated() {
n == 0 ? path.move(to: pt) : path.addLine(to: pt)
}
path.closeSubpath()
return path
}
}
struct StackedElementsView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这是它的外观,但不是我想要的:
我希望做的是将内部红色阴影仅放在白色描边上,因此描边只有内部有红色阴影而不在形状中。类似于这个: 我尝试在颜色本身上应用内阴影,但由于它不是一个形状,所以无法编译:
.stroke(Color.white.innerShadow(color: .red, radius: 2)
如何在描边颜色上应用内阴影?