【问题标题】:Why am I getting the error "Instance method 'fill(_:style:)' requires that 'some View' conform to 'ShapeStyle'"?为什么我收到错误“实例方法 'fill(_:style:)' 要求 'some View' 符合 'ShapeStyle'”?
【发布时间】:2022-01-27 08:00:59
【问题描述】:

我正在我的 SwiftUI 应用程序中制作加载屏幕,直到内容显示为止,屏幕将在渐变内为白色和灰色设置动画。

当我尝试运行此代码时出现错误,Instance method 'fill(_:style:)' requires that 'some View'符合 'ShapeStyle' 出现在我的 RoundedRectangle 旁边我不知道为什么?

有什么想法吗?谢谢

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)
    }
}

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    问题在于 onApear() 修饰符。它返回一些视图,并且填充修饰符需要符合 ShapeStyle 协议的视图。

    onApear 将在您的 ForEach 循环中多次触发。

    将其移至最外层范围:

    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()
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      相关资源
      最近更新 更多