我试图在按下按钮时向右移动一个文本字段。它工作正常,但是当我单击另一个文本字段或任何其他元素时,文本字段会重新出现在其当前位置。我浏览了互联网,发现当我没有启用自动布局时它可以工作,但是我需要自动布局来设置可变位置。有什么建议吗?下面是移动框的方法:
- (IBAction)EnterInfo:(id)sender {
CGRect newFrame = _UsernameField.frame;
newFrame.origin.x += 500; // shift right by 500pts
[UIView animateWithDuration:1.0
animations:^{
_UsernameField.frame = newFrame;
}];
}
最后一个问题的新代码:
- (IBAction)EnterInfo:(id)sender {
[UIView animateWithDuration:1.0
animations:^{
_usernameX.constant = 130;
}];
}
最佳答案
如果你打开了自动布局,你就不能那样操作框架。您需要对 NSLayoutConstraint
的引用并更新常量。
像这样:
myXConstraint.constant = originalX + 500;
编辑——然后您的动画 block 应该如下所示:
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
关于ios - 运行动画后文本字段返回到原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27912359/