我正在尝试将一个简单的 uiview 向上移动 30 点。但是,无论我做什么我都无法移动它。
我在 iOS 7.1 中开发
现在,我终于发现我启用了自动布局,但我对此 View 没有任何限制。这是一个简单的从对象库中拖动并放置在 View Controller 中的过程。我尝试了各种方法让它移动。我可以为属性设置动画,例如 alpha 和你拥有的东西,但我终究不能移动它。
下面是我试过的:
[UIView animateWithDuration:1.0f animations:^{
CGREct oldFrame = self.viewWantingToMove.frame;
self.viewWantingToMove.frame = CGRectMake(self.viewWantingToMove.frame.origin.x, self.viewWantingToMove.origin.y, self.viewWantingToMove.size.width, self.viewWantingToMove.width.height);
});
这行不通
[UIView animateWithDuration:1.0f animation:^{
self.viewWantingToMove.center = CGPoint(self.viewWantingToMove.frame.origin.x, self.viewWantingToMove.frame.origin.y-30
}];
再次纳达...
在我认为它可能具有自动布局功能后,请注意我无法将其关闭,因为我必须使用它,然后我尝试了这个。
[self.viewWantingToMove layoutIfNeeded];
[UIView animateWithDuration:1.0f animation:^{
self.viewWantingToMove.center = CGPoint(self.viewWantingToMove.frame.origin.x, self.viewWantingToMove.frame.origin.y-30
[self.viewWantingToMove layoutIfNeeded];
}];
这给了我动画……但是倒退了。它从向上 30 点开始,然后移回原来的位置。谁能帮我这个?它进入了你想要咬显示器的那些时刻之一。
最佳答案
当您使用自动布局时,您不应设置任何框架。要移动或调整 View 大小,您应该改为修改约束。您没有添加任何约束的事实确实意味着它们不存在。系统会为您添加默认约束。你应该设置你想要的约束,这样你就知道它们是什么(尝试在 View 的顶部、左侧以及高度和宽度的约束)。为 View 顶部的约束创建一个 IBOutlet(我们称之为 topCon),然后您可以在代码中修改它以使其具有动画效果,
[UIView animateWithDuration:1.0f animation:^{
self.topCon.constant -= 30;
[self.view layoutIfNeeded];
}];
关于ios - 动画时 UIView 不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23026023/