在执行较长任务时,在SWIFT IOS应用程序中加载覆盖功能的示例是什么?从远程服务器加载数据的示例. 我用谷歌搜索了一下,但没有找到任何答案.

更新:

感谢@Sebastian Dressler,这是一种简单的方式.我更新了我的代码,它运行起来很酷

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()

推荐答案

以上答案添加了一个加载视图,但它不会阻止屏幕上的点击事件,也不会为屏幕的其余部分提供覆盖.您可以通过以下方式实现:

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)

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 and newer

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)

您可以按如下方式隐藏它:

dismiss(animated: false, completion: nil)

它将显示如下: enter image description here

Ios相关问答推荐

如何让AVAudio.installTap()每秒回调125次

我如何确保用户继续他们在应用程序中停止的地方?

在SWIFT中将圆角+透明背景从导入视频(.mp4)添加到导出视频(.mov)

底部导航栏在iOS上浮动

使用SWIFT传输表示时的条件文件表示格式

自定义UIControl不适用于UITapGestureRecognizer

如何使用自定义数据源在本地react 本机 ios 应用程序中加载 Tradingview 小部件?

Swift UI中如何为Text提供内部填充

不可发送类型 '[String : Any]?'在调用非隔离实例方法 XXX 时退出主参与者隔离上下文不能跨越参与者边界

如何更改 SwiftUI 弹出视图的大小?

滚动 swiftUI 列表时,未调用单元格的任务修饰符.怎么修?

如何在Collection 后更改并保留按钮 colored颜色 ,然后在 SwiftUI 中切换回来?

如何根据设备时间设置 Flutter 应用时间线?

如何快速设置条形按钮的图像?

Select UITableViewCell 时 UIView backgroundColor 消失

如何关闭情节提要弹出框

无法将NSTaggedPointerString类型的值转换为NSNumber

在 iPhone 中将 UIViewController 显示为弹出窗口

aps-environment 始终在发展

如何防止 iOS 设备上的显示屏变暗和关闭?