我正在使用事件指示器向用户指示正在进行的网络调用。但是当网络调用完成时,我调用 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
}
}
最佳答案
根据上面给出的评论,我发现了问题所在。 stopAnimating()
是从后台线程调用的,并没有立即在处理器上执行。为了解决这个问题,我使用以下代码在主线程上调度了 stopAnimating()
。
dispatch_async(dispatch_get_main_queue()){
ProgressAnimationView.instance.stopAnimating()
}
关于ios - 事件指示器需要时间才能停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37572826/