[英]Why am I getting the error "Instance method 'fill(_:style:)' requires that 'some View' conform to 'ShapeStyle'"?
I am making a loading-screen in my SwiftUI app until the content shows, the screen will animate white and gray colors inside of a gradient.我正在我的 SwiftUI 应用程序中制作加载屏幕,直到内容显示,屏幕将在渐变内为白色和灰色 colors 设置动画。
When I try to run this code I get the error, Instance method 'fill(_:style:)' requires that 'some View' conform to 'ShapeStyle' that appear adjacent to my RoundedRectangle and I can't figure out why?当我尝试运行此代码时出现错误,实例方法“填充(_:样式:)”要求“某些视图”符合与我的 RoundedRectangle 相邻出现的“ShapeStyle” ,我不知道为什么?
Any ideas?有任何想法吗? THANKS
谢谢
struct LoadingMediaLibrary: View {
@State private var animateGradient = false
let size = UIScreen.main.bounds.width / 3.057
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(0..<4, id: \.self) { _ in
RoundedRectangle(cornerRadius: 20.00)
.fill(
LinearGradient(gradient: Gradient(colors: [Color.white, Color.gray]), startPoint: .top, endPoint: .bottom)
.onAppear {
withAnimation(.linear(duration: 2.0).repeatForever(autoreverses: true)) {
animateGradient.toggle()
}
}
)
.frame(width: size, height: size)
}
}
}.padding(.leading, 10).padding(.top, 10)
}
}
The problem is the onApear() modifier.问题是 onApear() 修饰符。 It returns some View and the fill modifier requiers a ShapeStyle protocol conform view.
它返回一些视图,并且填充修改器需要符合 ShapeStyle 协议的视图。
The onApear will be fired multiple times in your ForEach loop. onApear 将在您的 ForEach 循环中多次触发。
Move it to most outer scope:将其移至最外侧的 scope:
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(0..<4, id: \.self) { _ in
RoundedRectangle(cornerRadius: 20.00)
.fill(
LinearGradient(gradient: Gradient(colors: [Color.white, Color.gray]), startPoint: .top, endPoint: .bottom)
)
.frame(width: size, height: size)
}
}
}
.padding(.leading, 10).padding(.top, 10)
.onAppear {
withAnimation(.linear(duration: 2.0).repeatForever(autoreverses: true)) {
animateGradient.toggle()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.