简体   繁体   中英

NVActivityIndicatorView not working on button press

I have the following code for a button in my application to test my implementation of NVActivityIndicatorView :

@IBAction func goButtonPressed(_ sender: Any) {
    self.startAnimatingActivityIndicator()
    sleep(2)
    self.stopAnimatingActivityIndicator()
}

The view controllers in my application also have this extension:

extension UIViewController: NVActivityIndicatorViewable {
    func startAnimatingActivityIndicator() {
        let width = self.view.bounds.width / 3
        let height = width
        let size = CGSize(width: width, height: height)
        startAnimating(size, message: "Loading...", type: NVActivityIndicatorType.circleStrokeSpin)
    }

    func stopAnimatingActivityIndicator() {
        self.stopAnimating()
    }
}

The loading animations work elsewhere in the same view controller (ie, the viewDidLoad() function) but for some reason I'm unable to get the loading animation to work on this button. The button is connected correctly as the application does sleep for the appropriate amount of time, but the loading animations fail to run.

Thanks in advance for the help!

@FryAnEgg coming in with the solution! The sleep(2) was preventing the loading animations from running.

Here's my updated code:

@IBAction func goButtonPressed(_ sender: Any) {
    self.startAnimatingActivityIndicator()

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
        self.stopAnimatingActivityIndicator()
    }
}

The Indicator won't spin because the main thread is asleep. Use a 2 second timer to turn off the spinning instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM