单击“登录”后添加活动指示器?

时间:2017-07-17 04:46:24

标签: ios swift uiactivityindicatorview

我想为我的Login VC添加一个活动指示器,以便用户在点击“登录”按钮后会看到该微调器。我做了多次尝试但都失败了。即使我输入了隐藏活动指示器的代码,它也只是在点击“登录”按钮之前保持动画。我删除了这些代码,并在下面有我的原始代码(没有活动指示符)。

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)
                }
            }
        }
    }

所以我知道第一步可能是将活动指示器拖到故事板中的VC,但下一步是什么?

7 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:3)

创建活动指示器的IBOUtlet,从Storyboard到Viewcontroller - Creating Outlet

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

user_input_numbers

当你想要启动时,请致电

activityIndicator.hidesWhenStopped = true;

并阻止动画制作 -

activityIndicator.startAnimating();

答案 2 :(得分:3)

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

然后在调用signin方法之前为其设置动画,并在完成处理程序

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

答案 3 :(得分:3)

您可以通过编程方式在班级中创建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)
                }
            }
        }

答案 4 :(得分:2)

     In your storyboard, you can find  checkbox.
  1. startsAnimating
  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)
            }
        }
    }
    

    }

答案 5 :(得分:1)

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

  1. LoginViewController班级添加

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

  2. viewDidLoad()添加以下内容

    myActivityIndi​​cator.hidesWhenStopped = true             myActivityIndi​​cator.center = view.center             view.addSubview(myActivityIndi​​cator)

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

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

答案 6 :(得分:1)

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

显示进度hud写入..

ERProgressHud.show()

隐藏进度hud写..

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)
        }
    }
}
}