Col*_*lin 2 class viewcontroller ios swift
从这个问题开始:从Swift中的任何类加载屏幕
问题:加载覆盖视图将显示但不会在调用hideOverlayView()时隐藏.然而奇怪的是,叠加在一段时间后消失(出现后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()
}
}
Run Code Online (Sandbox Code Playgroud)
并在DataManager.swift中调用函数:
LoadingOverlay.shared.showOverlay()
Run Code Online (Sandbox Code Playgroud)
解:
我正在调用后台线程.根据以下答案,请致电:
dispatch_async(dispatch_get_main_queue(), { // This makes the code run on the main thread
LoadingOverlay.shared.hideOverlayView()
})
Run Code Online (Sandbox Code Playgroud)
斯威夫特2
你是hideOverlayView()从后台线程调用的吗?如果你是,你应该确保它在主线程上运行:
dispatch_async(dispatch_get_main_queue(), { // This makes the code run on the main thread
LoadingOverlay.shared.hideOverlayView()
})
Run Code Online (Sandbox Code Playgroud)
Swift 3+
DispatchQueue.main.async {
LoadingOverlay.shared.hideOverlayView()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8599 次 |
| 最近记录: |