加载一个";“覆盖”;在iOS中运行长任务时
在执行长任务时,在Swift IOS应用程序中加载覆盖的示例是什么。从远程服务器加载数据的示例。 我在谷歌上搜索了一下,但没有找到任何答案 更新: 感谢@Sebastian Dressler这是一个简单的方法。我更新了我的代码,它运行得很酷加载一个";“覆盖”;在iOS中运行长任务时,ios,swift,uiview,overlay,Ios,Swift,Uiview,Overlay,在执行长任务时,在Swift IOS应用程序中加载覆盖的示例是什么。从远程服务器加载数据的示例。 我在谷歌上搜索了一下,但没有找到任何答案 更新: 感谢@Sebastian Dressler这是一个简单的方法。我更新了我的代码,它运行得很酷 public class LoadingOverlay{ var overlayView = UIView() var activityIndicator = UIActivityIndicatorView() class var shared: Loa
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(view: UIView) {
overlayView.frame = CGRectMake(0, 0, 80, 80)
overlayView.center = view.center
overlayView.backgroundColor = UIColor(hex: 0x444444, alpha: 0.7)
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)
view.addSubview(overlayView)
activityIndicator.startAnimating()
}
public func hideOverlayView() {
activityIndicator.stopAnimating()
overlayView.removeFromSuperview()
}
}
让我们使用:
LoadingOverlay.shared.showOverlay(self.view)
//To to long tasks
LoadingOverlay.shared.hideOverlayView()
只需自己创建一个覆盖视图,将其添加到父视图中,并在任务完成后将其删除,例如添加:
var overlay : UIView? // This should be a class variable
[ ... ]
overlay = UIView(frame: view.frame)
overlay!.backgroundColor = UIColor.blackColor()
overlay!.alpha = 0.8
view.addSubview(overlay!)
删除:
overlay?.removeFromSuperview()
为了补充给出的答案,如果有时试图运行代码,可能会遇到问题。就个人而言,有一次showOverlay没有被正确调用(因为我试图进入一个场景,然后在viewDidLoad期间立即调用这个函数) 如果您遇到与我类似的问题,我建议您对代码进行一次修复,并改变方法 修复:将两个代码块作为闭包放置到dispatch_异步调用中,如下所示:
dispatch_async(dispatch_get_main_queue(),
{ //code });
方法:调用代码时,在调用后对主队列执行分派,将调用延迟几毫秒
原因是什么?您只是要求UI在viewDidLoad期间执行太多操作
如果这个解决方案的附录有帮助,我会很高兴
-乔尔朗
p.S.解决方案适用于XCode v6.3.2对于像我这样迟到的人,我对@Sonrobby代码做了一些修改。据我所知,@Sonrobby会在每次
showOverlay
调用时将活动添加到覆盖中。一些配置可以传递给init函数,只允许放置在showOverlay
方法上
我还将覆盖层的背景改为黑色,因为我的应用程序大部分是白色的
代码如下:
public class LoadingOverlay{
var overlayView : UIView!
var activityIndicator : UIActivityIndicatorView!
class var shared: LoadingOverlay {
struct Static {
static let instance: LoadingOverlay = LoadingOverlay()
}
return Static.instance
}
init(){
self.overlayView = UIView()
self.activityIndicator = UIActivityIndicatorView()
overlayView.frame = CGRectMake(0, 0, 80, 80)
overlayView.backgroundColor = UIColor(white: 0, alpha: 0.7)
overlayView.clipsToBounds = true
overlayView.layer.cornerRadius = 10
overlayView.layer.zPosition = 1
activityIndicator.frame = CGRectMake(0, 0, 40, 40)
activityIndicator.center = CGPointMake(overlayView.bounds.width / 2, overlayView.bounds.height / 2)
activityIndicator.activityIndicatorViewStyle = .WhiteLarge
overlayView.addSubview(activityIndicator)
}
public func showOverlay(view: UIView) {
overlayView.center = view.center
view.addSubview(overlayView)
activityIndicator.startAnimating()
}
public func hideOverlayView() {
activityIndicator.stopAnimating()
overlayView.removeFromSuperview()
}
}
上面的答案添加了一个加载视图,但它不会阻止屏幕上的点击事件,也不会为屏幕的其余部分提供覆盖。您可以通过以下方式实现:
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .Alert)
alert.view.tintColor = UIColor.blackColor()
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(10, 5, 50, 50)) as UIActivityIndicatorView
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
presentViewController(alert, animated: true, completion: nil)
dismiss(animated: false, completion: nil)
Swift 3.0
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
present(alert, animated: true, completion: nil)
Swift 4.0及更新版本
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
present(alert, animated: true, completion: nil)
您可以按如下方式隐藏它:
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .Alert)
alert.view.tintColor = UIColor.blackColor()
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(10, 5, 50, 50)) as UIActivityIndicatorView
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
presentViewController(alert, animated: true, completion: nil)
dismiss(animated: false, completion: nil)
将显示如下:
雨燕3
我在下面的回答中使用了@Lucho的代码,我将叠加背景颜色更改为清晰,并添加了微调器颜色
public class LoadingOverlay {
var overlayView : UIView!
var activityIndicator : UIActivityIndicatorView!
class var shared: LoadingOverlay {
struct Static {
static let instance: LoadingOverlay = LoadingOverlay()
}
return Static.instance
}
init(){
self.overlayView = UIView()
self.activityIndicator = UIActivityIndicatorView()
overlayView.frame = CGRect(0, 0, 80, 80)
overlayView.backgroundColor = .clear
overlayView.clipsToBounds = true
overlayView.layer.cornerRadius = 10
overlayView.layer.zPosition = 1
activityIndicator.frame = CGRect(0, 0, 40, 40)
activityIndicator.center = CGPoint(overlayView.bounds.width / 2, overlayView.bounds.height / 2)
activityIndicator.activityIndicatorViewStyle = .whiteLarge
activityIndicator.color = .gray
overlayView.addSubview(activityIndicator)
}
public func showOverlay(view: UIView) {
overlayView.center = view.center
view.addSubview(overlayView)
activityIndicator.startAnimating()
}
public func hideOverlayView() {
activityIndicator.stopAnimating()
overlayView.removeFromSuperview()
}
}
我创建了一个协议,用于将您自己的视图控制器显示为覆盖。用法非常简单:
class ViewController: UIViewController, OverlayHost {
@IBAction func showOverlayButtonPressed() {
showOverlay(type: YourOverlayViewController.self,
fromStoryboardWithName: "Main")
}
}
结果:
源代码:
相关文章:更新了@sonrobby answer,通过调整掩码大小添加了背景视图和方向处理。。。这可以用于简单的东西
public class LoadingOverlay{
var overlayView = UIView()
var activityIndicator = UIActivityIndicatorView()
var bgView = UIView()
class var shared: LoadingOverlay {
struct Static {
static let instance: LoadingOverlay = LoadingOverlay()
}
return Static.instance
}
public func showOverlay(view: UIView) {
bgView.frame = view.frame
bgView.backgroundColor = UIColor.gray
bgView.addSubview(overlayView)
bgView.autoresizingMask = [.flexibleLeftMargin,.flexibleTopMargin,.flexibleRightMargin,.flexibleBottomMargin,.flexibleHeight, .flexibleWidth]
overlayView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
overlayView.center = view.center
overlayView.autoresizingMask = [.flexibleLeftMargin,.flexibleTopMargin,.flexibleRightMargin,.flexibleBottomMargin]
overlayView.backgroundColor = UIColor.black
overlayView.clipsToBounds = true
overlayView.layer.cornerRadius = 10
activityIndicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
activityIndicator.activityIndicatorViewStyle = .whiteLarge
activityIndicator.center = CGPoint(x: overlayView.bounds.width / 2, y: overlayView.bounds.height / 2)
overlayView.addSubview(activityIndicator)
view.addSubview(bgView)
self.activityIndicator.startAnimating()
}
public func hideOverlayView() {
activityIndicator.stopAnimating()
bgView.removeFromSuperview()
}
}
如果您将其添加到keywindow,它还可以浏览导航栏和选项卡栏。。。像这样的
LoadingOverlay.shared.showOverlay(view: UIApplication.shared.keyWindow!)
模糊背景+活动指示器,Swift 5示例
extension UIView {
func showBlurLoader() {
let blurLoader = BlurLoader(frame: frame)
self.addSubview(blurLoader)
}
func removeBluerLoader() {
if let blurLoader = subviews.first(where: { $0 is BlurLoader }) {
blurLoader.removeFromSuperview()
}
}
}
class BlurLoader: UIView {
var blurEffectView: UIVisualEffectView?
override init(frame: CGRect) {
let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = frame
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.blurEffectView = blurEffectView
super.init(frame: frame)
addSubview(blurEffectView)
addLoader()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func addLoader() {
guard let blurEffectView = blurEffectView else { return }
let activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
blurEffectView.contentView.addSubview(activityIndicator)
activityIndicator.center = blurEffectView.contentView.center
activityIndicator.startAnimating()
}
}
使用ATKit
参考:
ATProgressOverlay类
代码:
截图:
Swift 5
class func showUniversalLoadingView(_ show: Bool, loadingText : String = "") {
let existingView = UIApplication.shared.windows[0].viewWithTag(1200)
if show {
if existingView != nil {
return
}
let loadingView = self.makeLoadingView(withFrame: UIScreen.main.bounds, loadingText: loadingText)
loadingView?.tag = 1200
UIApplication.shared.windows[0].addSubview(loadingView!)
} else {
existingView?.removeFromSuperview()
}
}
class func makeLoadingView(withFrame frame: CGRect, loadingText text: String?) -> UIView? {
let loadingView = UIView(frame: frame)
loadingView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
//activityIndicator.backgroundColor = UIColor(red:0.16, green:0.17, blue:0.21, alpha:1)
activityIndicator.layer.cornerRadius = 6
activityIndicator.center = loadingView.center
activityIndicator.hidesWhenStopped = true
activityIndicator.style = .white
activityIndicator.startAnimating()
activityIndicator.tag = 100 // 100 for example
loadingView.addSubview(activityIndicator)
if !text!.isEmpty {
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
let cpoint = CGPoint(x: activityIndicator.frame.origin.x + activityIndicator.frame.size.width / 2, y: activityIndicator.frame.origin.y + 80)
lbl.center = cpoint
lbl.textColor = UIColor.white
lbl.textAlignment = .center
lbl.text = text
lbl.tag = 1234
loadingView.addSubview(lbl)
}
return loadingView
}
使用
showUniversalLoadingView(真实)
卸下加载器
showUniversalLoadingView(false)
@Ajinkya Patil回答作为参考Swift 4.0及更新版本
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
present(alert, animated: true, completion: nil)
这是一个扩展解决方案,可在所有viewController上使用,不会发生冲突
创建加载对话框+ViewContoller.swift
import UIKit
struct ProgressDialog {
static var alert = UIAlertController()
static var progressView = UIProgressView()
static var progressPoint : Float = 0{
didSet{
if(progressPoint == 1){
ProgressDialog.alert.dismiss(animated: true, completion: nil)
}
}
}
}
extension UIViewController{
func LoadingStart(){
ProgressDialog.alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.gray
loadingIndicator.startAnimating();
ProgressDialog.alert.view.addSubview(loadingIndicator)
present(ProgressDialog.alert, animated: true, completion: nil)
}
func LoadingStop(){
ProgressDialog.alert.dismiss(animated: true, completion: nil)
}
}
调用ViewController中的函数,可以在任意位置调用。像这样:
self.LoadingStart()
下面是如何停止加载对话框
self.LoadingStop()
Xamarin.iOS版本:
var-alert=UIAlertController.Create(string.Empty,“请稍候…”,UIAlertControllerStyle.alert);
var alertIndicatorView=新UIActivityIndicatorView();
alertIndicatorView.Frame=新的CGRect(x:10,y:5,宽度:50,高度:50);
alertIndicatorView.ActivityIndicatorViewStyle=UIActivityIndicatorViewStyle.Gray;
alertIndicatorView.HidesWhenStopped=true;
alertIndicatorView.StartAnimating();
添加(alertIndicatorView);
控制器。PresentViewController(警报、真、空);
谢谢@Sebastian Dressler,我刚刚更新了我的问题。你能把上面的代码翻译成Swift吗?不,我不这么认为。我在这里提供的内容可以用作基本框架。只需将其余部分添加到顶部即可。截至今天(2015年11月),似乎需要覆盖!。removeFromSuperView()[注意感叹号而不是问号]可以确认
在overlay…
removeFromSuperview()中的小“v”几乎无法满足您的请求。而你也得不到一个简单的“翻译”。如果你在谷歌上搜索,例如使用Objective-C覆盖,你会发现一些条目如何在iOS/OS X上实现。dismissViewControllerAnimated(false,completion:nil)在XCode 8.2中不存在。它已更改为Disclose(动画:false,completion:nil)一种更安全的解除方式是:internal func dismissAlert(){如果让vc=self.presentedViewController,vc是UIAlertController{Disclose(动画:false,completion:nil)}
对于Swift 4.0选项,我收到此警告:警告:尝试显示不在窗口层次结构中的视图代码>工作正常,但在关闭后**navigationController?.pushViewController(adminHomeVC,动画:true)**
不工作。。(swift 4)在swift 4中将loadingIndicator.style
更改为loadingIndicator.activityIndicatorViewStyle
,如何使用它?啊,谢谢,我真的得到了它,在uiviewcontroller中添加了self.view.addSubview(overlay)伟大的解决方案!这个答案非常有用-如果其他使用Xamarin.iOS的人需要这样做,我已经用C#重写了它。你可以在viewController中使用它来显示self.view.showBlurLoader()和删除self.view.removeBluerLoader(),使用bgView.frame=view.bounds
?因为view.frame.origin
可能不同于(0,0)。覆盖的目的是覆盖提供给它的视图…它不必关心原点在哪里。。。。如果您需要覆盖整个屏幕,请将其应用于keywindow。如果您遇到过is,请告诉我