ios - UIActivityIndicator 添加 AppDelegate
问题描述
我想在打开应用程序时运行 UIActivityIndicator。我怎样才能做到这一点?我的代码是我在那里分享的方式。我希望此代码在 Launchscreen 打开时工作。
@objc func buttonTapped(_ sender: UIButton) {
let size = CGSize(width: 30, height: 30)
let selectedIndicatorIndex = sender.tag
let indicatorType = presentingIndicatorTypes[selectedIndicatorIndex]
startAnimating(size, message: "Loading...", type: indicatorType, fadeInAnimation: nil)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5) {
NVActivityIndicatorPresenter.sharedInstance.setMessage("Authenticating...")
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
self.stopAnimating(nil)
}
}
for (index, indicatorType) in presentingIndicatorTypes.enumerated() {
let x = 150
let y = 250
let frame = CGRect(x: x, y: y, width: 100, height: 100)
let activityIndicatorView = NVActivityIndicatorView(frame: frame,
type: .orbit)
let animationTypeLabel = UILabel(frame: frame)
activityIndicatorView.padding = 20
if indicatorType == NVActivityIndicatorType.orbit {
activityIndicatorView.padding = 0
}
self.view.addSubview(activityIndicatorView)
self.view.addSubview(animationTypeLabel)
activityIndicatorView.startAnimating()
let button: UIButton = UIButton(frame: frame)
button.tag = index
#if swift(>=4.2)
button.addTarget(self,
action: #selector(buttonTapped(_:)),
for: .touchUpInside)
#else
button.addTarget(self,
action: #selector(buttonTapped(_:)),
for: UIControlEvents.touchUpInside)
#endif
self.view.addSubview(button)
}
解决方案
您要实现的是UIActivityIndicator
在启动屏幕期间显示,不幸的是这是不可能的。如果您想在启动屏幕上做任何动画,请使用您的第一个视图控制器作为启动屏幕,并在启动屏幕上的动画结束时重定向到主屏幕。
一些想法:
- 创建单独的加载页面并从
didFinishLaunchingWithOptions
App 委托中的方法调用它 - 向它添加加载指示器(可以是任何你想要的)
- 将计时器设置为 2-3 秒,当它结束时重定向到您的主屏幕
PS 延迟应用程序启动只是为了向用户展示一些很棒的动画/加载屏幕并不是一个好主意。对于第一次使用应用程序的用户来说,它可能看起来很酷等等,但是随着应用程序的广泛使用,用户可能会在每次启动应用程序时等待几秒钟而感到沮丧。
祝你好运 :)
推荐阅读
- reactjs - 在 React 中,存储大数据数组的更好地方是什么?
- powershell - 按用户属性过滤 AD 组;退货不当
- python - 按间隔对数据帧进行分组
- qt - OpenCV4 - 深度神经网络 - dnn.hpp 没有这样的文件或目录
- python - 如何根据环境从我的 settings.py DATABASES 数组中激活数据库设置?
- c# - 许多按钮触发 backgroundWorker
- git - 从 Powershell 运行“Git-Clone”,即使它似乎可以工作,也会出现错误
- xamarin.forms - 如何从 xamarin 应用程序中删除蓝色?
- c++ - 在各种“if”语句中做同样的事情,可以改进吗?
- python - 在布尔值的二维列表中找到最左边和最右边的单元格(Python)