gpt4 book ai didi

ios - 在 Swift 中添加和删除 View 覆盖

转载 作者:可可西里 更新时间:2023-11-01 00:23:34 25 4
gpt4 key购买 nike

根据这个问题:Loading Screen from any Class in Swift

问题:当调用 hideOverlayView() 时,加载覆盖 View 会显示但不会隐藏。然而奇怪的是,覆盖层会在一段时间后消失(出现后 15 到 30 秒)

代码:包含在FirstController.swift

public class LoadingOverlay{

var overlayView = UIView()
var activityIndicator = UIActivityIndicatorView()

class var shared: LoadingOverlay {
struct Static {
static let instance: LoadingOverlay = LoadingOverlay()
}
return Static.instance
}

public func showOverlay() {
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate,
let window = appDelegate.window {
overlayView.frame = CGRectMake(0, 0, 80, 80)
overlayView.center = CGPointMake(window.frame.width / 2.0, window.frame.height / 2.0)
overlayView.backgroundColor = MyGlobalVariables.UICOLORGREEN
overlayView.clipsToBounds = true
overlayView.layer.cornerRadius = 10

activityIndicator.frame = CGRectMake(0, 0, 40, 40)
activityIndicator.activityIndicatorViewStyle = .WhiteLarge
activityIndicator.center = CGPointMake(overlayView.bounds.width / 2, overlayView.bounds.height / 2)

overlayView.addSubview(activityIndicator)
window.addSubview(overlayView)

activityIndicator.startAnimating()
}
}

public func hideOverlayView() {
activityIndicator.stopAnimating()
overlayView.removeFromSuperview()
}
}

并在 DataManager.swift 中调用函数:

LoadingOverlay.shared.showOverlay()

解决方案:

我正在调用后台线程。根据以下答案,调用为:

dispatch_async(dispatch_get_main_queue(), { // This makes the code run on the main thread
LoadingOverlay.shared.hideOverlayView()
})

最佳答案

swift 2

您是从后台线程调用 hideOverlayView() 吗?如果是,则应确保它在主线程上运行:

dispatch_async(dispatch_get_main_queue(), { // This makes the code run on the main thread
LoadingOverlay.shared.hideOverlayView()
})

swift 3+

DispatchQueue.main.async {
LoadingOverlay.shared.hideOverlayView()
}

关于ios - 在 Swift 中添加和删除 View 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33064908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com