ios - 运行动画后文本字段返回到原始位置

标签 ios objective-c animation

我试图在按下按钮时向右移动一个文本字段。它工作正常,但是当我单击另一个文本字段或任何其他元素时,文本字段会重新出现在其当前位置。我浏览了互联网,发现当我没有启用自动布局时它可以工作,但是我需要自动布局来设置可变位置。有什么建议吗?下面是移动框的方法:

- (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/

相关文章:

ios - Swift 4 UIActivityIndi​​catorView 不以 UITableView 为中心

css - 动画播放状态重置或恢复

jquery - 限制 jQuery-ui Draggable 只能在给定区域内向上拖动

iphone - 按下时 UILongPressGestureRecognizer 被调用两次

ios - 你能在另一个 iOS 应用程序中运行一个 iOS 应用程序吗

objective-c - 编译时出错 : No visible @interface declares the selector

cocoa-touch - UITableView中动画插入/删除的2个NSArray的差异

ios - 可操作通知 - 第三个操作/按钮不起作用

ios - 如何将分数垂直显示为 2 列

ios - 我可以为两个 UIButton 设置相同的标签吗