我有以下代码:
override func viewDidLoad() {
super.viewDidLoad()
...
signInButton = CustomButton(frame: CGRectMake(12.5/100.0 * self.view.bounds.size.width, 5/6.0 * self.view.bounds.size.height, 60, 23))
signInButton.customizeButtonText("Sign in")
joinButton = CustomButton(frame: CGRectMake((75/100.0 * self.view.bounds.size.width), 5/6.0 * self.view.bounds.size.height, 60, 23))
joinButton.customizeButtonText("Join")
userNameTextField = CustomTextField(frame: CGRectMake(-500, 60/100.0 * self.view.bounds.size.height, 400, 35))
userNameTextField.placeholder = "Username"
passwordTextField = CustomTextField(frame: CGRectMake(-500, 70/100.0 * self.view.bounds.size.height, 400, 35))
passwordTextField.placeholder = "Password"
signInButton.addTarget(self, action: "signInButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
joinButton.addTarget(self, action: "joinButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
}
然后在 signInButtonClicked:
和 joinButtonClicked:
方法中我有这段代码,我将 UITextField 设置为我想要的特定 x 和 y 位置:
func signInButtonClicked(sender: UIButton!) {
UIView.animateWithDuration(0.25, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.userNameTextField.frame = CGRectMake((-500 + ((50/100.0 * self.view.bounds.size.width) - 200)), 60/100.0 * self.view.bounds.size.height, 400,35)
}, completion: nil)
}
func joinButtonClicked(sender: UIButton!) {
UIView.animateWithDuration(0.25, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.passwordTextField.frame = CGRectMake((-500 + ((50/100.0 * self.view.bounds.size.width) - 200)), 70/100.0 * self.view.bounds.size.height, 400,35)
}, completion: nil)
}
出于某种原因,每当我点击 signInButton
或 joinButton
时,文本字段的动画不会发生。我在这里做错了什么吗?我将两个按钮的目标添加到 self 并为每个按钮设置正确的操作。
自定义按钮类:
import UIKit
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.layer.cornerRadius = 2.0
self.layer.shadowColor = UIColor(red: SHAWDOW_COLOR, green: SHAWDOW_COLOR, blue: SHAWDOW_COLOR, alpha: 0.5).CGColor
self.layer.shadowRadius = 5.0
self.layer.shadowOpacity = 1
self.layer.shadowOffset = CGSizeMake(0.0, 2.0)
self.titleLabel?.font = UIFont(name: "NotoSans-Regular", size: 17.0)
self.backgroundColor = UIColor.redColor()
self.userInteractionEnabled = true
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func customizeButtonText(text: String!) {
self.setTitle(text, forState: UIControlState.Normal)
}
}
最佳答案
您的代码(请说:应该)有效;不起作用的是您在这些操作方法中设置动画位置的方式。
通过查看您设置 UITextFields
初始位置和动画后位置的方式,我了解到您希望 UITextFields
最初在屏幕外,然后在单击按钮后在屏幕上显示动画。
鉴于此,问题出在以下代码行:
self.userNameTextField.frame = CGRectMake((((-500 + 50/100.0 * self.view.bounds.size.width) - 200)), 60/100.0 * self.view.bounds.size.height, 400, 35)
self.passwordTextField.frame = CGRectMake((((-500 + 50/100.0 * self.view.bounds.size.width) - 200)), 70/100.0 * self.view.bounds.size.height, 400,35)
记录这些数字,人们会看到:
NSLog("%f", (-500 + ((50/100.0 * self.view.bounds.size.width) - 200)))
-512.500000
功能确实有效,动画也是如此;只是您将 UITextField
从最初位于屏幕外的位置设置为动画,好吧,又是另一个位于屏幕外的位置。
通过简单地将动画 block 中的行分别更改为以下内容:
self.userNameTextField.frame = CGRectMake((((50/100.0 * self.view.bounds.size.width) - 200)), 60/100.0 * self.view.bounds.size.height, 400, 35)
self.passwordTextField.frame = CGRectMake((((50/100.0 * self.view.bounds.size.width) - 200)), 70/100.0 * self.view.bounds.size.height, 400,35)
允许我们在单击按钮后看到 UITextField
确实进入屏幕。
关于ios - TouchUpInside 按钮不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36106703/