在 Swift 中添加和删除视图覆盖
Adding & Removing A View Overlay in Swift
根据这个问题:Loading Screen from any Class in 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()
}
}
并在 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()
}
根据这个问题:Loading Screen from any Class in 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()
}
}
并在 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()
}