当用户从段输入中选择一个选项时,我必须隐藏一个字段。 隐藏的字段是第一个,因此我必须为其他字段设置动画以覆盖由隐藏字段创建的空间。
我正在使用以下代码,动画在隐藏手机输入后按预期工作。但当我从同一视图中选择任何其他字段(电子邮件或任何按钮或其他任何内容)时,emailInput将再次显示在之前的相同位置。动画的影响(输入的位置)将丢失。
@IBAction func hasPhoneChanged(sender: UISegmentedControl) {//is called when user selects that "he have phone or not"
if sender.selectedSegmentIndex == 0 {//Yes
self.phoneInput.hidden = false
...
}else{ //No
self.phoneInput.hidden = true
//move the next input up, to cover space
UIView.animateWithDuration(1, animations: { () -> Void in
self.emailInput.frame.origin.y = self.emailInput.frame.origin.y - 38;
})
}
}
我做错了吗?任何线索请。
答案 0 :(得分:2)
如果使用自动布局,请改为为自动布局约束设置动画。 例如:
UIView.animateWithDuration(1, animations: { () -> Void in
self.emailTopVSpaceConstraint.constant = self.emailTopVSpaceConstraint.constant - 38;
self.view.layoutIfNeeded()
})