【问题标题】:Show UIView/UIImage/UITextView while function is running在函数运行时显示 UIView/UIImage/UITextView
【发布时间】:2016-11-26 18:52:50
【问题描述】:

目标

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

问题

在处理下面的代码时,UIView、UIImage 和 UITextView 直到函数 完成 运行(而不是 显示当函数完成时,函数开始运行并隐藏)。

目前的方法:

我创建了一个 UIView (loadingView),其中包含图像 (loadingIcon) 和一个 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)
}

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

【问题讨论】:

  • 使用完成处理程序隐藏您的视图

标签: ios swift uiview uiimage activity-indicator


【解决方案1】:

您正在主队列上运行该进程,因此您的 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)
        }
    }
}

【讨论】:

  • 非常感谢@rmaddy!
  • 作为对任何其他有类似问题的人的有用提示,我在上面的“//处理信息,这需要一些时间”部分的末尾以编程方式调用了一个 segue。通过将此调用移至主线程 ("DispatchQueue.main.asyc{ ... }") 效果很好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-23
  • 2014-05-11
相关资源
最近更新 更多