gpt4 book ai didi

ios - 当 UIActivityIndi​​catorView 加载速度足够快(不到一秒)时不显示它

转载 作者:行者123 更新时间:2023-11-28 08:35:09 25 4
gpt4 key购买 nike

我想知道是否可以在数据加载速度足够快(例如不到一秒)时不显示 UIActivityIndi​​catorView

在我当前的应用中,我的收藏 View 会在每次新搜索时重新加载,但有时搜索速度非常快,指示器 View 会闪烁并立即消失,给应用带来丑陋的整体感觉。

所以基本上我的问题是:当我的加载足够快时,我可以“禁用”UIActivityIndi​​catorView 吗?

最佳答案

使用此类,如果回调发生时间少于 1 秒,我不会显示事件指示器。

final class DelayableActivityIndicatorView: UIActivityIndicatorView {
static let kDefaultDelay: TimeInterval = 1
private var inProgress = false

func startAnimatingWithDelay(_ delay: TimeInterval = kDefaultDelay) {
inProgress = true
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
if self.inProgress == true {
self.startAnimating()
}
self.inProgress = false
}
}

func stopAnimatingWithDelay() {
inProgress = false
stopAnimating()
}
}

在 View Controller 中使用它,

class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white

self.view = view
}

var spinner: DelayableActivityIndicatorView = {
let view = DelayableActivityIndicatorView(style: .gray)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(spinner)
spinner.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
spinner.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

spinner.startAnimatingWithDelay()

// replace this code with your network api
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.spinner.stopAnimatingWithDelay()
}
}
}

关于ios - 当 UIActivityIndi​​catorView 加载速度足够快(不到一秒)时不显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37963863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com