【发布时间】:2020-10-14 22:44:33
【问题描述】:
我是一个初学者 SwiftUI 程序员,我遇到了以下问题:
我有两个视图,一个加载视图(矩形)和一个网页视图(WKWebView)。
像这样使用这两个视图:
var body : some View {
ZStack {
WebView(self.webView)
Rectangle()
.foregroundColor(self.state.isLoading ? self.loadingColor : Color.clear)
}
.onAppear {
// Initialize web view etc.
// Sets self.state.isLoading to false after some time has passed
}
}
有趣的是,当self.state.isLoading 从true 更改为false 时,UI 会短暂闪烁白色。
我不知道为什么会这样,特别是因为 WebView 在显示时肯定不是白色的。
我尝试添加背景矩形并设置self.webView和self.webView.scrollView的backgroundColor:
var body : some View {
ZStack {
Rectangle()
.foregroundColor(Color.red)
WebView(self.webView)
Rectangle()
.foregroundColor(self.state.isLoading ? self.loadingColor : Color.clear)
}
.onAppear {
// Initialize web view etc.
self.webView.backgroundColor = UIColor.red
self.webView.scrollView.backgroundColor = UIColor.red
// Sets self.state.isLoading to false after some time has passed
}
}
我预计会看到一个短暂的红屏,但它仍然是白色的。
在 Rectangle() 过渡中添加动画使闪烁消失
Rectangle()
.foregroundColor(self.state.isLoading ? self.loadingColor : Color.clear)
.animation(.easeIn)
我是否遗漏了什么或做错了什么?
【问题讨论】: