简体   繁体   English

为什么我收到错误“实例方法 'fill(_:style:)' 要求 'some View' 符合 'ShapeStyle'”?

[英]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.

相关问题 错误:实例方法 'onReceive(_:perform:)' 要求 'Int' 符合 'Publisher' - Error: Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher' 实例方法“flatMap”要求“String”符合“ObservableConvertibleType” - Instance method 'flatMap' requires that 'String' conform to 'ObservableConvertibleType' 为什么会出现错误“类型ViewController不符合协议UIPageViewControllerDataSource”? - Why am i getting the error 'Type ViewController does not conform to protocol UIPageViewControllerDataSource'? 定义方法后,为什么会收到“无法识别的选择器发送到实例”错误? - Why am I getting an “unrecognised selector sent to instance” error when I have defined the method? VStack 显示错误“实例方法 'sheet(item:onDismiss:content:)' 要求 'Int' 符合 'Identifiable'” - VStack shows error "Instance method 'sheet(item:onDismiss:content:)' requires that 'Int' conform to 'Identifiable'" 为什么我得到一个“无法识别的选择器发送到实例”错误? - Why am I getting a `unrecognized selector sent to instance` error? 在这种情况下,为什么我会收到一条消息发送到已释放实例错误? - Why am I getting a message sent to deallocated instance error in this case? 收到“选择器的未知实例方法”错误,我不知道为什么 - Getting a“No Known instance method for selector” error and I have no idea why 我认为我在分配segmentcontrol时犯了一些错误。我遇到了一个错误[无法识别的选择器发送到实例] - I think i have done some mistake in allocating segmentcontrol..i am getting an error [unrecognized selector sent to instance] 使用&#39;lazy var&#39;创建实例时,为什么会出现编译器错误? - Why am I getting a compiler error when using 'lazy var' to create an instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM