Activity 指标在执行时不显示
Activity indicator not showing when executed
我试图在用户点击登录按钮时显示 activity 指示器。如果我将 startActivityIndicator()
代码放在 viewDidLoad()
中,它会完全按预期显示在屏幕上。当我将它作为 btnSignIn()
中的第一步执行时,它从未出现过。有点迷茫,所以我希望 Stack 大师可以提供帮助...
// 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()
}
使用我的 delay
实用程序(参见此处:),像这样重写:
@IBAction func btnSignIn(sender: AnyObject) {
startActivityIndicator()
delay(0.1) {
if validateEmailAddress(txtEmailAddress.text!) == false {
// ... everything else goes here ...
}
}
延迟使 activity 指标有机会出现并开始旋转。
我试图在用户点击登录按钮时显示 activity 指示器。如果我将 startActivityIndicator()
代码放在 viewDidLoad()
中,它会完全按预期显示在屏幕上。当我将它作为 btnSignIn()
中的第一步执行时,它从未出现过。有点迷茫,所以我希望 Stack 大师可以提供帮助...
// 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()
}
使用我的 delay
实用程序(参见此处:),像这样重写:
@IBAction func btnSignIn(sender: AnyObject) {
startActivityIndicator()
delay(0.1) {
if validateEmailAddress(txtEmailAddress.text!) == false {
// ... everything else goes here ...
}
}
延迟使 activity 指标有机会出现并开始旋转。