2ank3th
2ank3th

Reputation: 3109

Activity Indicator takes time to stop animating

I am using activity indicator to indicate ongoing network call to the user. But when network call is completed I call stopAnimating to stop activity Indicator. But activator indicator stay on screen for another 1 to 3 seconds before it disappears.

PS: I am sure no one else is calling activator indicator methods.

    class ProgressAnimationView: UIView
{

    static var instance = ProgressAnimationView()
    // MARK: - Properties -

    var backgroundView: UIView
    var activityIndicator: UIActivityIndicatorView

    // MARK: - Methods -

    required override init(frame: CGRect) {

        backgroundView = UIView()
        backgroundView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
        activityIndicator = UIActivityIndicatorView()
        activityIndicator.hidesWhenStopped = true
        super.init(frame: frame)



       let vc =  UIApplication.sharedApplication().windows.first?.rootViewController
        backgroundView.frame = vc!.view.bounds
        activityIndicator.center = backgroundView.center
        vc?.view.addSubview(backgroundView)
        vc?.view.addSubview(activityIndicator)
    }

    convenience required init(coder aDecoder: NSCoder) {
        self.init(frame: CGRectZero)
    }


    // MARK: - Progress Animation



    func startAnimating() {
        activityIndicator.startAnimating()
        backgroundView.hidden = false
    }

    func stopAnimating() {

        activityIndicator.stopAnimating()
        backgroundView.hidden = true
    }

}

Upvotes: 0

Views: 1687

Answers (1)

2ank3th
2ank3th

Reputation: 3109

Based on comments given above, I found the issue. stopAnimating() was being called from a background thread and was not immediately executed on the processor. To solve the issue I dispatched stopAnimating() on the main thread using following code.

    dispatch_async(dispatch_get_main_queue()){
        ProgressAnimationView.instance.stopAnimating()
    }

Upvotes: 3

Related Questions