我正在尝试在两个文本字段输入文本后为 UIButton 设置动画。即使没有输入文本,按钮也会动画。我认为这可能是存在占位符文本的问题,但这没有效果。我试过 != ""和 != nil
这是我的代码
override func viewDidLoad() {
super.viewDidLoad()
self.title = ""
textFieldConfig()
loginButtonConfig("LOG IN")
}
func loginButtonConfig(title:String) {
let frameWidth = self.view.frame.width
let frameHeight = self.view.frame.height
var button = UIButton()
button.frame = CGRect(x: 0, y: 300, width: 0, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: 0, height: 50)
button.backgroundColor = UIColor(red: 24.0/255.0, green: 198.0/255.0, blue: 152.0/255.0, alpha: 1.0)
button.setTitle(title, forState: .Normal)
button.titleLabel?.textAlignment = NSTextAlignment.Center
self.view.addSubview(button)
//WHY ISN'T THE IF STATEMENT WORKING AS IT SHOULD???
if self.userTextField.text != nil && self.passwordTextField.text != nil {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}
}
如有任何帮助,我们将不胜感激。谢谢:)
最佳答案
试试这个:
if !self.userTextField.text.isEmpty && !self.passwordTextField.isEmpty {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}
关于ios - Swift - 如果语句不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875280/