活动指示器在执行时未显示 [英] Activity indicator not showing when executed

查看:82
本文介绍了活动指示器在执行时未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户点击登录按钮时显示活动指示符。如果我在 viewDidLoad()中放入 startActivityIndi​​cator()代码,它会在屏幕上完全按预期显示。当我作为 btnSignIn()中的第一步执行它时,它永远不会出现。有点失落,所以我希望Stack大师能帮忙...

I am trying to display an activity indicator when the user hits the login button. If I put the startActivityIndicator() code in viewDidLoad() it shows on the screen exactly as expected. When I execute it as the first step in btnSignIn() it never appears. A little lost, so i'm hoping the Stack guru's can help...

// Here are the variable declarations
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
var loadingView: UIView = UIView()
var viewCenter:CGPoint!

@IBAction func btnSignIn(sender: AnyObject) {

    startActivityIndicator()

    if validateEmailAddress(txtEmailAddress.text!) == false {
        stopActivityIndicator(self.loadingView)
        return
    }

    if validatePassword(txtPassword.text!) == false {
        stopActivityIndicator(self.loadingView)
        return
    }

    PFUser.logInWithUsernameInBackground(txtEmailAddress.text!, password:txtPassword.text!) {
        (user: PFUser?, error: NSError?) -> Void in
        if user != nil {

            // Successful login.
            self.txtPassword.resignFirstResponder()
            self.txtEmailAddress.resignFirstResponder()

            self.getUserInfo()

        } else {
            self.stopActivityIndicator(self.loadingView)
            // The login failed. Display alert.
            self.displayAlert("Whoops!", message: "Email or Password are incorrect.")
        }
    }
}

func startActivityIndicator() {

    loadingView.frame = CGRectMake(0, 0, 80, 80)
    loadingView.center = viewCenter
    print(viewCenter)
    loadingView.backgroundColor = UIColorFromRGB("444444", alpha: 0.7)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10

    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    activityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2);

    view.addSubview(loadingView)
    loadingView.addSubview(activityIndicator)

    UIApplication.sharedApplication().beginIgnoringInteractionEvents()

    activityIndicator.startAnimating()

}

func stopActivityIndicator(uiView: UIView) {

    activityIndicator.stopAnimating()
    loadingView.removeFromSuperview()
    UIApplication.sharedApplication().endIgnoringInteractionEvents()
}


推荐答案

使用我的延迟实用程序(请参阅此处: https ://stackoverflow.com/a/24318861/341994 ),重写如下:

Using my delay utility (see here: https://stackoverflow.com/a/24318861/341994), rewrite like this:

@IBAction func btnSignIn(sender: AnyObject) {
    startActivityIndicator()
    delay(0.1) {
        if validateEmailAddress(txtEmailAddress.text!) == false {
        // ... everything else goes here ...
    }
}

延迟使活动指示器有机会出现并开始旋转。

The delay gives the activity indicator a chance to appear and start spinning.

这篇关于活动指示器在执行时未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆