Ios 动态移动(设置动画)UITextField
我一直在尝试设置UITextField的动画,使其y位置增加50px。基本上,它向上移动了50个像素。这是我的代码:Ios 动态移动(设置动画)UITextField,ios,swift,xcode6.4,Ios,Swift,Xcode6.4,我一直在尝试设置UITextField的动画,使其y位置增加50px。基本上,它向上移动了50个像素。这是我的代码: @IBAction func textField(sender: AnyObject) { let x = self.pw.frame.origin.x let y = self.pw.frame.origin.y + 100 UIView.animateWithDuration(0.5, delay: 0, options: nil, animation
@IBAction func textField(sender: AnyObject) {
let x = self.pw.frame.origin.x
let y = self.pw.frame.origin.y + 100
UIView.animateWithDuration(0.5, delay: 0, options: nil, animations: {
self.pw.frame = CGRectMake(x, y, self.pw.frame.size.width, self.pw.frame.size.height)
}, completion: nil)
它位于文本字段的委托中。此代码在点击UITextField时运行
遗憾的是,这段代码并没有达到我想要的效果。运行时,它会将文本字段向上移动50个像素,然后再向下向右移动。它并没有在应该是它的最终位置的地方结束。你期望
pw
帧移动100,但是在设备/模拟器上它只有50,这是由于像素和点之间的差异(看一看)。
对于返回其原始位置的框架,您应该检查代码的其余部分。如注释中所述,安装了自动布局约束后,您不应该手动修改框架。相反,您需要更改约束以反映动画的最终结果 下面是一个简单的工作示例。它创建一个按钮和一个文本字段,并最初将文本字段定位在按钮末端下方58点处。单击该按钮时,文本字段顶部间距约束上的常数将从58减小到8,以向上移动文本字段
class ViewController: UIViewController {
var textFieldTopSpacingConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
// Create the button
let button = UIButton.buttonWithType(.System) as! UIButton
button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.setTitle("Tap me!", forState: .Normal)
button.addTarget(self, action: "buttonTapped", forControlEvents: .TouchUpInside)
view.addSubview(button)
// Create the text field
let textField = UITextField()
textField.placeholder = "Enter text here"
textField.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addSubview(textField)
let views: [NSObject: AnyObject] = ["button": button, "textField": textField]
// Layout the button
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[button]-|", options: nil, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[button(44)]", options: nil, metrics: nil, views: views))
// Layout the text field and remember its top spacing constraint
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[textField]-|", options: nil, metrics: nil, views: views))
textFieldTopSpacingConstraint = NSLayoutConstraint(item: textField, attribute: .Top, relatedBy: .Equal,
toItem: button, attribute: .Bottom, multiplier: 1, constant: 58) // The text field starts 58 points below the end of the button
view.addConstraint(textFieldTopSpacingConstraint!)
}
func buttonTapped() {
// Change to constant on the top spacing constraint to move the text field up
UIView.animateWithDuration(0.5) {
self.textFieldTopSpacingConstraint?.constant = 8 // The text field now starts 8 points below the end of the button
self.view.layoutIfNeeded()
}
}
}
通过Interface Builder设置约束后,您可以在视图控制器中为顶部间距约束创建一个出口,并使用该出口修改文本字段的顶部空间。是否还有其他方法可以在代码中定位文本字段?也许在超级视图的布局子视图中?有自动调整大小的掩码或自动布局限制吗?@hennes-我有限制。我是否应该在viewDidLoad()中手动设置这些约束?安装约束后,不应该手动操作框架。您需要通过更改约束来执行动画。你能分享一下你对文本字段的限制吗?@hennes-水平(在视图中)和垂直间距到一个UIButton他在问题中没有说100而不是50,还是我错了?