【问题标题】:Parent tell child, that child is allowed to download image家长告诉孩子,孩子可以下载图片
【发布时间】:2020-12-27 14:13:18
【问题描述】:

这大概是我的代码的样子。这样你就知道我在做什么了。在父视图中,我有另一个用户可以单击并选择的电视频道列表。之后,用户可以执行 DragUp 手势并显示 ListOfChildren。每个孩子都持有一个图像,该图像是从 web 动态加载的。每个电视频道都有大约 10 个孩子,它必须下载并显示该图像。这就是为什么我只想在用户执行 DragUp 手势以显示 ListOfChildren 时才允许下载和显示图像。

我怎样才能从家长那里通知孩子,它可以开始下载图片了?

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

【问题讨论】:

标签: image asynchronous dynamic swiftui scrollview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多