【发布时间】:2019-11-21 00:15:17
【问题描述】:
我想用某种修饰符调用我的 ImageView 以使其中的图像为圆形或方形。代码应如下所示:
ImageView(withURL: newsItem.imageUrl).test(Circle())
要获得我读到的这种行为,您可以像下面那样扩展视图。它与 String 完美搭配,但我不明白 Shape 有什么不同?
我收到错误消息 Protocol type 'Shape' cannot conform to 'Shape' 因为只有具体类型才能符合协议,但我也不明白:/。有人可以解释一下这个问题以及我能做些什么吗?
struct ImageView: View {
var clipShape: Shape
var body: some View {
ZStack {
Image("swift")
.resizable()
.aspectRatio(contentMode: .fill)
.clipShape(self.clipShape)
.shadow(radius: 6)
}
}
}
extension ImageView {
func test(_ shape: Shape) -> Self {
var copy = self
copy.clipShape = shape
return copy
}
}
顺便说一句,此代码被缩短为仅显示特定问题的代码,视图包含的内容不止于此。只是为了让您不会质疑它的用途。
提前致谢!
编辑:
为什么形状不起作用以及该怎么做请看:https://forums.swift.org/t/how-to-make-different-clipshape/27448/2 在我的情况下,如果发现只需对整个 ImageView 进行剪辑整形就可以了。 我还想链接这个Creating custom modifiers for Swift UI views,您可以在其中阅读有关自定义修饰符的信息
【问题讨论】: