单击 "LOGIN" 后添加 Activity 指标?

Adding Activity Indicator after Clicking "LOGIN"?

我想为我的登录 VC 添加一个 Activity 指示器,这样用户在单击 "login" 按钮后就会看到那个微调器。我做了多次尝试但都失败了。即使我输入了隐藏 activity 指示器的代码,它也只是在单击 "login" 按钮之前保持动画。我删除了那些代码,下面是我的原始代码(没有 activity 指标)。

import UIKit
import Firebase

class LoginViewController: UIViewController {
    var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView = UIImageView(frame: view.bounds)
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.image = #imageLiteral(resourceName: "background")
        imageView.center = view.center
        view.addSubview(imageView)
        self.view.sendSubview(toBack: imageView)
    }

    //Outlets
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!

    //Login Action
    @IBAction func loginAction(_ sender: AnyObject) {
        if self.emailTextField.text == "" || self.passwordTextField.text == "" {
            //Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in

            let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)

            self.present(alertController, animated: true, completion: nil)
        } else {
            Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
                if error == nil {
                    //Print into the console if successfully logged in
                    print("You have successfully logged in")

                    //Go to the HomeViewController if the login is sucessful
                    let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                    self.present(vc!, animated: true, completion: nil)
                } else {
                    //Tells the user that there is an error and then gets firebase to tell them the error
                    let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)

                    self.present(alertController, animated: true, completion: nil)
                }
            }
        }
    }

所以我知道第一步可能是将 activity 指示器拖到 Storyboard 中的 VC,但下一步是什么?

您需要为拖动的 UIActivityIndi​​cator 创建一个 IBOutlet。然后在 viewDidLoadfunc 中用它的 IBOutlet 隐藏这个 UIActivityIndi​​cator。当您单击“登录”按钮时,一旦收到登录响应,就会取消隐藏此 activityIndi​​cator 并再次隐藏。

为您的 activity 指标创建一个 IBOUtlet,从 Storyboard 到您的 Viewcontroller -

然后您可以在您的 ViewDidLoad 或故事板中设置以下 属性

activityIndicator.hidesWhenStopped = true;

当你想启动它时,调用

activityIndicator.startAnimating();

并停止动画 -

activityIndicator.stopAnimating();

与您创建 UITextField 的 IBOutlets 的方式相同,用您的 UIActivityIndicator 创建一个。确保您的指标 hidesWhenStopped 在故事板中设置为 true。

然后在调用您的登录方法之前对其进行动画处理,并在完成处理程序上停止它

@IBOutlet weak var activityIndicator: UIActivityIndicator!
//...
activityIndicator.startAnimating()
Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in {
activityIndicator.stopAnimating()
//...
}

您可以在 class 中以编程方式创建 UIActivityIndi​​catorView 并在 viewDidLoad

中自定义它
 var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)

  // Add below code in viewDidLoad

 self.activityIndicator.hidesWhenStopped = true
 self.activityIndicator.center = view.center
 self.view.addSubView(self.activityIndicator)

现在开始和停止任何你需要的动画

 //Login Action
    @IBAction func loginAction(_ sender: AnyObject) {
        self.activityIndicator.startAnimating()
        if self.emailTextField.text == "" || self.passwordTextField.text == "" {

            //Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in

            let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)

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

        } else {

            Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
                self.activityIndicator.stopAnimating()
                if error == nil {

                    //Print into the console if successfully logged in
                    print("You have successfully logged in")

                    //Go to the HomeViewController if the login is sucessful
                    let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                    self.present(vc!, animated: true, completion: nil)

                } else {

                    //Tells the user that there is an error and then gets firebase to tell them the error
                    let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)

                    self.present(alertController, animated: true, completion: nil)
                }
            }
        }

另一种方法。以编程方式添加 UIActivityViewController

  1. LoginViewControllerclass中添加

    让 myActivityIndi​​cator = UIActivityIndi​​catorView(activityIndi​​catorStyle: UIActivityIndicatorViewStyle.gray)

  2. viewDidLoad()中添加以下内容

    myActivityIndicator.hidesWhenStopped = 真 myActivityIndicator.center = view.center view.addSubview(myActivityIndi​​cator)

  3. @IBAction func loginAction(_ sender: AnyObject)中的else部分 添加

    activityIndicator.startAnimating() Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in { activityIndicator.stopAnimating()

我写了一个 class 来正确使用进度 HUD。您只需要将 class 拖放到您的项目中... https://github.com/emraz/ERProgressHud

为了显示进度 hud 写..

ERProgressHud.show()

为了隐藏进度hud write ..

ERProgressHud.hide()

在你的代码中..

//登录动作

@IBAction func loginAction(_ sender: AnyObject) {

if self.emailTextField.text == "" || self.passwordTextField.text == "" {

    //Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in
    let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)

    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
    alertController.addAction(defaultAction)

    self.present(alertController, animated: true, completion: nil)
    return

}
else {

    ERProgressHud.show()

    Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in

        ERProgressHud.hide()
        if error == nil {

            //Print into the console if successfully logged in
            print("You have successfully logged in")

            //Go to the HomeViewController if the login is sucessful
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
            self.present(vc!, animated: true, completion: nil)

        } else {

            //Tells the user that there is an error and then gets firebase to tell them the error
            let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)

            self.present(alertController, animated: true, completion: nil)
        }
    }
}
}
     In your storyboard, you can find  checkbox.
  1. 开始动画
  2. HidesWhenStops(在你的情节提要中检查。)

     @IBOutlet weak var activityIndicator: UIActivityIndicator!
    
     @IBAction func loginAction(_ sender: AnyObject) {
     activityIndicator.startAnimating()
    
     if self.emailTextField.text == "" || self.passwordTextField.text == "" {
        //Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in
    
          let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)
         activityIndicator.stopAnimating()
        let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertController.addAction(defaultAction)
    
        self.present(alertController, animated: true, completion: nil)
    } else {
        Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
            if error == nil {
                //Print into the console if successfully logged in
                print("You have successfully logged in")
                activityIndicator.stopAnimating()
                //Go to the HomeViewController if the login is sucessful
                let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                self.present(vc!, animated: true, completion: nil)
            } else {
                //Tells the user that there is an error and then gets firebase to tell them the error
                let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
    
                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)
    
                self.present(alertController, animated: true, completion: nil)
            }
        }
    }
    

    }