活动指示器被按钮阻止

时间:2016-11-03 07:36:09

标签: ios swift uiview

我在我的按钮上添加了一个活动指示器,并在按下按钮时为其设置动画,但是,每次动画都是动画,但是无法看到,这是我的代码:

DispatchQueue.global(qos: .userInitiated).async {
        self.openCircle(withCenter: sender.center, dataSource: self.calculator!.iterateWPItems()){}
        DispatchQueue.main.sync {
            self.activityIndicator.stopAnimating()
        }
    }
    activityIndicator.startAnimating()

2 个答案:

答案 0 :(得分:0)

只需在completion handler中写一下。无需使用dispatch_after

        self.activityIndicator.startAnimating()
    print(activityIndicator.isAnimating)

    openCircle(withCenter: sender.center, dataSource: calculator!.iterateWPItems()){

// place webservice data processing here
        dispatch_async(dispatch_get_main_queue()) { 
                activityIndicator.stopAnimating();
        }
    }

斯威夫特3:

    self.activityIndicator.startAnimating()
    print(activityIndicator.isAnimating)

    openCircle(withCenter: sender.center, dataSource: calculator!.iterateWPItems()){
// place webservice data processing here

        DispatchQueue.main.async { 
          self.activityIndicator.stopAnimating()

        }
    }

答案 1 :(得分:0)

假设您的openCircle方法是异步方法。我认为当您收到方法回叫时,您应该停止活动指示。请尝试修改此代码。

self.activityIndicator.startAnimating()
print(activityIndicator.isAnimating)

openCircle(withCenter: sender.center, dataSource: calculator!.iterateWPItems()){
    dispatch_async(dispatch_get_main_queue()) { 
            activityIndicator.stopAnimating();
    }
}

对于Swift 3,

self.activityIndicator.startAnimating()
print(activityIndicator.isAnimating)

openCircle(withCenter: sender.center, dataSource: calculator!.iterateWPItems()){
    DispatchQueue.main.async { 
        self.activityIndicator.stopAnimating()
    }
}

使用NSOperation,

self.activityIndicator.startAnimating()
print(activityIndicator.isAnimating)

openCircle(withCenter: sender.center, dataSource: calculator!.iterateWPItems()){
    OperationQueue.main.addOperation {
       self.activityIndicator.stopAnimating()
    }
}