如何编写 Clipshape 的函数签名?

问题描述 投票:0回答:1

我有这个修改器

.clipShape(RoundedRectangle(cornerRadius: 25,
                            style: .continuous)

但我想创建一个函数来替换它

.clipShape(myFunction())

在函数内部我可能有类似的东西

func myFunction() -> ????? {
  RoundedRectangle(cornerRadius: 25,
                   style: .continuous).stroke(.green, width:1)
}

首先,我该如何退货以及我穿了什么??????

我已经尝试过了

func myFunction() -> ????? {
  let shape = RoundedRectangle(cornerRadius: 25,
                   style: .continuous).stroke(.green, width:1)
  return shape
}

我已经尝试过,按照Xcode的建议

@ViewBuilder
func myFunction() -> any Shape {
  let shape = RoundedRectangle(cornerRadius: 25,
                   style: .continuous).stroke(.green, width:1)
  return shape
}

但是 Shape 是一个协议。

像往常一样,苹果的零文档。他们为什么要打扰?

有什么想法吗?谢谢。

swiftui struct shapes
1个回答
0
投票

使用

some Shape

func myFunction() -> some Shape {
  RoundedRectangle(
    cornerRadius: 25,
    style: .continuous
  )
}

请注意,我删除了

stroke
修饰符。用颜色抚摸
Shape
会产生不是
Shape
的视图,因此不能像
clipShape
那样使用。

© www.soinside.com 2019 - 2024. All rights reserved.