将 NVActivityIndi​​catorView 与 Timer 一起使用,它显示的时间很短

Using NVActivityIndicatorView with Timer, it shows short time

我用NVActivityIndi​​catorView创建了一个loading页面,翻页标签屏5秒

A​​ctivityIndi​​catorView 是短暂的并且很快消失。 使用计时器,我让事件在五秒钟内发生。 Timer.scheduledTimer()用过 DispatchQueue.main.async{}。 我以为它运行主线程。我改了timeInterval,没变。

   DispatchQueue.main.async {

          self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)
          self.performLogin()
     }

代码

import UIKit
import NVActivityIndicatorView

class AutoLoginViewController: UIViewController, NVActivityIndicatorViewable {

    var timer: Timer?
    var indicatorView: NVActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()
        setup()

        getToken(username: "yoshimi", password: "11111")

    }


    override func viewWillDisappear(_ animated: Bool) {
        stopIndicator()
    }
    // MARK: - Setup
    private func setup() {
        var midY = self.view.frame.height / 2
        var midX = self.view.frame.width / 2
        let frame = CGRect(x: midX, y: midY, width: 30, height: 30)

        indicatorView = NVActivityIndicatorView(frame: frame,
                                                    type: .ballScaleRippleMultiple)
        indicatorView?.tintColor = .white

    }

    // MARK: - Segue
    private func performLogin() {

        guard
             let tabBarController = R.storyboard.main.ramAnimatedTabBarController(),
             let mainViewController = tabBarController.viewControllers?.first as? MainViewController

        else {
              return
        }

      self.present(tabBarController, animated: true, completion: nil)

    }
    // MARK: - run indicator

    @objc func timerAction() {

        let size = CGSize(width: 30, height: 30)
        UIView.animate(withDuration: 5.0) {
            self.indicatorView.startAnimating()
            self.startAnimating(size,message: "Loading...", type: .ballScaleRippleMultiple, fadeInAnimation: nil)
        }
          timer?.invalidate()
      }

    private func stopIndicator() {

        self.indicatorView.stopAnimating()
        self.stopAnimating(nil)

    }

    // MARK: - APIs
    private func getToken(username: String?, password: String?) {
        guard
            let username = username,
            let password = password
        else {
            return
        }

        API.LoginClass.getToken(username: username, password: password) { (token, success) in
            guard success, let token = token else {
                self.loginError()
                return
            }

            Configure.token = token
            self.login(username: username, password: password)
        }



        DispatchQueue.main.async {

          self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)
          self.performLogin()
     }

    }
}

你可以试试这个

self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)

     DispatchQueue.main.async {

             self.performLogin()
     }