6

我试图在按下按钮后显示活动指示器视图,但它不想显示。如果我根本不将其设置为隐藏,它会显示,当我尝试隐藏它时,它会隐藏。但如果它被隐藏,它将不会再次显示。

这是代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    loading.hidden = true
}

@IBAction func submit() {
    loading.hidden = false
    loading.startAnimating()
    if chosenCategory == "" || txtName.text == "" || txtEmail.text == ""    {
        loading.stopAnimating()
        loading.hidden = true
    } else {
        println("animation")

无论如何,停止动画有效,它可以隐藏,但无论我做什么,它似乎只是跳过loading.hidden = false并直接将消息打印出来。

会发生什么?

4

2 回答 2

17

我建议根本不要使用.hidden属性。请改用.hidesWhenStopped属性并将其设置为true。然后只需调用.startAnimating()方法,UIActivityIndi​​catorView 就会自动变为可见并开始动画。调用.stopAnimating()方法,UIActivityIndi​​catorView 会自动隐藏。

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    loading.hidesWhenStopped = true //this is all you need to change
}

@IBAction func submit() {
    loading.startAnimating() // becomes visible
    if chosenCategory == "" || txtName.text == "" || txtEmail.text == ""    {
        loading.stopAnimating() // goes into hiding
    } else {
        println("animation")
于 2016-06-26T08:53:40.263 回答
6

使用 GCD (Grand Central Dispatch) 例程:

    self.activityIndicator.startAnimating()
    self.activityIndicator.hidden = false

    dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), { ()->() in
        ... // your long running code here

        self.activityIndicator.stopAnimating()
    })
于 2014-09-09T18:53:33.533 回答