我有一個 CustomShapeView,它基本上采用 Shape 內容,因此在匯入 CustomShapeView 后,我想使用一個適用于 Shape 的修飾符,但在我的情況下它沒有!我想知道我之間缺少什么?
我的目標:我希望能夠匯入一個 Shape 并在 CustomShapeView 中應用 strokeBorder 修飾符并將其作為視圖回傳。
struct ContentView: View {
var body: some View {
CustomShapeView(shapeContent: {
RoundedRectangle(cornerRadius: 50.0)
})
.padding()
}
}
struct CustomShapeView<ShapeContent: Shape>: View {
let shapeContent: () -> ShapeContent
var body: some View {
return shapeContent()
//.strokeBorder(style: StrokeStyle(lineWidth: 10.0)) // <<: Here!!! Why?
}
}
uj5u.com熱心網友回復:
設定InsettableShape通用約束。
struct CustomShapeView<ShapeContent: InsettableShape>: View { // << == Here
let shapeContent: () -> ShapeContent
直接看strokeBorder定義,wherestrokeBorder是在InsettableShape協議里面定義的,哪個是繼承的Shape。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368972.html
下一篇:在Swift中型別轉換為泛型類
