功能为 运行 时显示 UIView/UIImage/UITextView

Show UIView/UIImage/UITextView while function is running

OBJECTIVE

我试图在函数 运行 时显示 UIView、UIImage 和 UITextView,以便让用户知道它正在处理(类似于 Activity 指示器,但更自定义).

问题

当下面的代码正在处理时,UIView、UIImage 和 UITextView 直到函数 完成 运行 前一刻才会显示(而不是 显示函数启动运行和隐藏当函数完成).

当前方法:

我创建了一个包含图像 (loadingIcon) 的 UIView (loadingView) 和一个 textView (loadingText),向用户说明该应用正在处理。

我还创建了一个名为 isLoading 的函数,它显示或隐藏所有 3 行,而不是多次重复这些行。我已经在 viewDidLoad 中测试了将 isLoading 设置为 true 和 false 以确保它正常工作。

@IBOutlet weak var loadingView: UIView!
@IBOutlet weak var loadingIcon: UIView!
@IBOutlet weak var loadingText: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    isLoading(false)
}


func isLoading(_ loadStatus: Bool) {
    if loadStatus == true {
        loadingView.isHidden = false
        loadingIcon.isHidden = false
        loadingText.isHidden = false
    } else {
        loadingView.isHidden = true
        loadingIcon.isHidden = true
        loadingText.isHidden = true
    }
}

@IBAction func sendButtonPressed(_ sender: AnyObject) {
    isLoading(true)

    ... //process information, which takes some time

    isLoading(false)
}

非常感谢任何帮助、建议或想法。谢谢。

您是 运行 主队列中的进程,因此您的 UI 似乎挂起,直到它完成。您需要在后台处理这些信息。您可能使用的常见模式是:

@IBAction func sendButtonPressed(_ sender: AnyObject) {
    isLoading(true)

    // Do the processing in the background    
    DispatchQueue.global(qos: .userInitiated).async {
        ... //process information, which takes some time

        // And update the UI on the main queue
        DispatchQueue.main.async {
            isLoading(false)
        }
    }
}