ios - 事件指示器需要时间才能停止动画

标签 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
    }

}

最佳答案

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

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

关于ios - 事件指示器需要时间才能停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37572826/

相关文章:

ios - 为什么当我的基础数据源更新时 SwiftUI 会自动向后导航,我该如何避免这种行为?

android - iOS 的 BluetoothSocket 相当于什么?

ios - 如何根据 iOS 版本加载多个 Storyboard文件? (5 和 6)

ios - 为什么单例类可以作为普通类使用

ios - 在 firebase 中插入而不是更新

swift - ARKit 中的 ScreenShot 问题在 swift 中

ios - 如何将对象放置在中心图像周围的阵列中?

ios - 为每个操作创建 Realm 实例的开销/问题?

iphone - 如何设置 UITableView 单元格 alpha?

ios - 使用键盘 ios 向上/向下移动 View