Parent告诉child,允许child下载图片

Parent tell child, that child is allowed to download image

这大概就是我的代码的样子。这样你就知道我在做什么。在 Parent 视图中,我有另一个用户可以点击的电视频道列表和 select。之后,用户可以执行 DragUp 手势并显示 ListOfChildren。每个 child 包含一个图像,该图像是从网络动态加载的。每个电视频道大约有 10 children,它必须下载并显示该图像。这就是为什么我只想在用户执行 DragUp 手势以显示 ListOfChildren 时才允许下载和显示图像。

如何从 Parent 通知 child 它可以开始下载图像?

struct Child : View {
    @State var imageURL: URL 
    @State var isAllowedToDownloadImage: bool

    var body : some View {
        // if(isAllowedToDownloadImage) { <- I want to notify child from parent, that
        //                                   child is allowed to download and display image
            URLImage(url: imageURL) 
        //}
    }
}

struct ListOfChildren : View {
    @State var children: [Child] = [Child]()
    var body : some View {
        ScrollView(.horizontal) {
            HStack {
                ForEach(children) { child in 
                    Child(imageURL: child.image)
                }
            }
        }
    }
}

struct Parent : View {
    var body : some View {
        ListOfChildren 
        // with drag gesture I show ListOfChildren and after it's shown
        // I want to notify child that's it's allowed to download and display image
        // DragGesture()...
    }
}

@lorem ipsum 朝着正确的方向迈出了一步。现在可以正常使用了,谢谢!

struct Parent : View {
    // when I change selected channel, i set appState.startDownloadingImages to false
    @EnvironmentObject var appState: AppState

    var body : some View {
        ListOfChildren 
            .gesture(DragGesture()
                .onEnded { _ in
                    // only change state if images hasn't started loading 
                    if(self.appState.startDownloadingImages == false ) {
                        self.appState.startDownloadingImages = true
                    } 
                })
        .environmentObject(self.appState)
    }
}

struct ListOfChildren : View {
    @State var children: [Child] = [Child]()
    @EnvironmentObject var appState: AppState

    var body : some View {
        ScrollView(.horizontal) {
            HStack {
                ForEach(children) { child in 
                    Child(imageURL: child.image, allowImageDownload: self.$appState.startDownloadingImages)
                }
            }
        }.environmentObject(self.appState)

    }
}

struct Child : View {
    @State var imageURL: URL 
    @Binding var allowImageDownload: bool

    var body : some View {

        // if imageURL is empty or if images are not yet allowed
        // to be downloaded, just show empty view
        if(self.imageURL == "" || !allowImageDownload) {
            VStack{}
        }
        else {
            URLImage(url: URL(string: self.imageURL!)!,
                content: { image in
                    image   
                }
            )        
        }
    }
}