![](/img/trans.png)
[英]Error: Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher'
[英]Why am I getting the error "Instance method 'fill(_:style:)' requires that 'some View' conform to 'ShapeStyle'"?
我正在我的 SwiftUI 應用程序中制作加載屏幕,直到內容顯示,屏幕將在漸變內為白色和灰色 colors 設置動畫。
當我嘗試運行此代碼時出現錯誤,實例方法“填充(_:樣式:)”要求“某些視圖”符合與我的 RoundedRectangle 相鄰出現的“ShapeStyle” ,我不知道為什么?
有任何想法嗎? 謝謝
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)
}
}
問題是 onApear() 修飾符。 它返回一些視圖,並且填充修改器需要符合 ShapeStyle 協議的視圖。
onApear 將在您的 ForEach 循環中多次觸發。
將其移至最外側的 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.