我最初用 xcode 10 开发的应用程序,但我已经升级到 xcode 11,然后加载器不隐藏检查下面的代码
import UIKit
class LoadingView: UIView {
//MARK: IndicatoreShow
class func Show() {
DispatchQueue.main.async {
let loadingView = LoadingView(frame: UIScreen.main.bounds)
loadingView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
if let _lastWindow = UIApplication.shared.windows.last {
if !_lastWindow.subviews.contains(where: { $0 is LoadingView }) {
_lastWindow.endEditing(true)
_lastWindow.addSubview(loadingView)
}
}
loadingView.addFadeAnimationWithFadeType(.fadeIn)
let indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
indicator.center = loadingView.center
indicator.tintColor = .white
if UI_USER_INTERFACE_IDIOM() == .pad {
indicator.style = .whiteLarge
} else {
indicator.style = .white
}
indicator.startAnimating()
loadingView.addSubview(indicator)
}
}
//MARK: IndicatoreHide
class func Hide() {
DispatchQueue.main.async {
if let _lastWindow = UIApplication.shared.windows.last {
for subview in _lastWindow.subviews {
if let loadingView = subview as? LoadingView {
loadingView.addFadeAnimationWithFadeType(.fadeOut)
}
}
}
}
}
}
//MARK: Animation Enum
enum FadeType {
case fadeIn
case fadeOut
}
extension UIView {
//MARK: AnimationWith Fade
func addFadeAnimationWithFadeType(_ fadeType: FadeType) {
switch fadeType {
//MARK: fade IN
case .fadeIn:
DispatchQueue.main.async {
self.alpha = 0.0
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.alpha = 1.0
})
}
//MARK: Fade Out
case .fadeOut:
UIView.animate(withDuration: 0.5, animations: { () -> Void in
DispatchQueue.main.async {
self.alpha = 0.0
}
}, completion: { (finished) -> Void in
if finished {
self.removeFromSuperview()
}
})
}
}
}
我用了 LoadView.Show((//这是工作 LoadView.Hide((//这在 iOS 13 中不起作用 我应该在代码中更改什么,因为许多代码在iOS 13中不起作用,例如状态栏背景颜色
在您发布的代码中,您的"淡出"块如下所示:
//MARK: Fade Out
case .fadeOut:
UIView.animate(withDuration: 0.5, animations: { () -> Void in
DispatchQueue.main.async {
self.alpha = 0.0
}
}, completion: { (finished) -> Void in
if finished {
self.removeFromSuperview()
}
})
}
在那里,用DispatchQueue.main.async
包装的唯一代码行是self.alpha = 0.0
.其他所有内容都将在调用它的线程上执行。
将该大小写更改为:
//MARK: Fade Out
case .fadeOut:
DispatchQueue.main.async {
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.alpha = 0.0
}, completion: { (finished) -> Void in
if finished {
self.removeFromSuperview()
}
})
}
}
可能会解决问题。