首页 > 解决方案 > UIActivityIndi​​cator 添加 AppDelegate

问题描述

我想在打开应用程序时运行 UIActivityIndi​​cator。我怎样才能做到这一点?我的代码是我在那里分享的方式。我希望此代码在 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)
        }

标签: iosswiftuiactivityindicatorview

解决方案


您要实现的是UIActivityIndicator在启动屏幕期间显示,不幸的是这是不可能的。如果您想在启动屏幕上做任何动画,请使用您的第一个视图控制器作为启动屏幕,并在启动屏幕上的动画结束时重定向到主屏幕。


一些想法:

  • 创建单独的加载页面并从didFinishLaunchingWithOptionsApp 委托中的方法调用它
  • 向它添加加载指示器(可以是任何你想要的)
  • 将计时器设置为 2-3 秒,当它结束时重定向到您的主屏幕

PS 延迟应用程序启动只是为了向用户展示一些很棒的动画/加载屏幕并不是一个好主意。对于第一次使用应用程序的用户来说,它可能看起来很酷等等,但是随着应用程序的广泛使用,用户可能会在每次启动应用程序时等待几秒钟而感到沮丧。


祝你好运 :)


推荐阅读