【发布时间】:2021-09-04 00:37:48
【问题描述】:
我想将属性列表应用于 VStack 而不是形状。以下代码通过在 Shape 扩展中定义的 innerN 函数将我的属性列表应用于形状:
ZStack {
Color.mph
let shape = RoundedRectangle(cornerRadius: 25)
//let shape = Circle() //it works with any shapes
shape
.innerN(Color.mph)
.frame(width: 300, height: 300)
}
extension Shape {
public func innerN<S:ShapeStyle>
(_ fillContent: S) -> some View
{
ZStack {
self.fill(fillContent)
.overlay(
self
.stroke(Color.gray, lineWidth: 4)
.blur(radius: 4)
.offset(x: 2, y: 2)
.mask(self.fill(LinearGradient(Color.black, Color.clear)))
)
.overlay(
self
.stroke(Color.white, lineWidth: 8)
.blur(radius: 4)
.offset(x: -2, y: -2)
.mask(self.fill(LinearGradient(Color.clear, Color.black)))
)
}
}
}
我怎样才能修改我的扩展程序,以便我可以做类似的事情? :
VStack(spacing: 20){//...}
.padding()
.cornerRadius(25)
//.innerN(color) //with the shape of that VStack
【问题讨论】:
-
如果你使用
extension View而不是Shape会发生什么?它适用于两者。