【发布时间】:2021-04-26 09:28:23
【问题描述】:
我遇到了一个问题,我的UIView 和Lottie-Animation 没有正确显示。我使用了很多 Dispatch.main.async,我很确定这会搞砸一切。
1. 用户点击Button 并立即显示我的UIVIew("coverView")+ Animation ("loadingAnimation")(调用setupLoadingAnimation)
但不是,它只是在另一个功能完成后才显示。
我必须如何以及在哪里调用animation.play() 和view.isHidden = false,以便在点击button 后立即显示?
这是我的结构:
@objc func addWishButtonTapped() {
print("tapped 1")
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
// ...
self.setUpLoadingAnimation()
// ...
print("tapped 2")
self.crawlWebsite {
print("tapped 3")
self.hideLoadingView()
print("tapped 4")
}
}
}
视图未设置,它在print("tapped 2") 之后停止printing,仅打印print(tapped 3")。那是view+ loadingAnimatino 变得可见的时候。
Setup Loading Animation:
//MARK: setupLoadingAnimation
func setUpLoadingAnimation(){
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.view.addSubview(self.coverView)
self.coverView.topAnchor.constraint(equalTo: self.wishView.topAnchor).isActive = true
self.coverView.leadingAnchor.constraint(equalTo: self.wishView.leadingAnchor).isActive = true
self.coverView.trailingAnchor.constraint(equalTo: self.wishView.trailingAnchor).isActive = true
self.coverView.bottomAnchor.constraint(equalTo: self.wishView.bottomAnchor).isActive = true
self.loadingAnimation.contentMode = .scaleAspectFit
self.loadingAnimation.translatesAutoresizingMaskIntoConstraints = false
self.coverView.addSubview(self.loadingAnimation)
self.loadingAnimation.centerXAnchor.constraint(equalTo: self.coverView.centerXAnchor).isActive = true
self.loadingAnimation.centerYAnchor.constraint(equalTo: self.coverView.centerYAnchor).isActive = true
self.loadingAnimation.heightAnchor.constraint(equalToConstant: 100).isActive = true
self.loadingAnimation.widthAnchor.constraint(equalToConstant: 100).isActive = true
self.loadingAnimation.loopMode = .loop
self.loadingAnimation.play()
}
}
crawlWebsite:
//MARK: crawlWebsite
func crawlWebsite(finished: @escaping () -> Void){
var html: String?
guard let url = self.url else { return }
let directoryURL = url as NSURL
let urlString: String = directoryURL.absoluteString!
// save url to wish
self.wishView.link = urlString
DispatchQueue.main.async { [weak self] in
guard let self = self else { finished(); return }
html = self.getHTMLfromURL(url: url)
self.getContentFromHTML(html: html, url: url)
self.getOpenGraphData(urlString: urlString) {
finished()
}
}
}
我知道这很混乱,所以我希望一切都清楚!如果您有任何问题,请告诉我!我被困在这里了......
【问题讨论】:
标签: ios swift asynchronous uiview lottie