Agregar y quitar una superposición de vista en Swift
Siguiendo de esta pregunta:Pantalla de carga de cualquier clase en Swift
Problema: Se mostrará la vista de superposición de carga, pero no se ocultará cuando se llame a hideOverlayView (). Curiosamente, sin embargo, la superposición desaparece después de un tiempo (15 a 30 segundos después de que aparece)
Código: Contenida enFirstController.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()
}
}
Y llamado en funciones enDataManager.swift como:
LoadingOverlay.shared.showOverlay()
Solución:
Estaba llamando a un hilo de fondo. Según las respuestas a continuación, llame como:
dispatch_async(dispatch_get_main_queue(), { // This makes the code run on the main thread
LoadingOverlay.shared.hideOverlayView()
})