活动指示器需要时间来停止动画

时间:2016-06-01 15:23:02

标签: ios swift

我正在使用活动指示器来指示正在进行的用户网络呼叫。但是当网络呼叫完成后,我呼叫stopAnimating停止活动指示器。但激活指示器在屏幕上再停留1到3秒才会消失。

PS:我确信没有其他人正在调用激活指示器方法。

    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
    }

}

1 个答案:

答案 0 :(得分:3)

根据上面给出的评论,我发现了这个问题。 stopAnimating()是从后台线程调用的,并没有立即在处理器上执行。为了解决这个问题,我使用以下代码在主线程上发送了stopAnimating()

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