iOS 6动画大小的视图和约束移动其他视图

时间:2013-05-05 23:21:17

标签: ios ios6 core-animation autolayout

我第一次玩iOS 6自动布局,我正在试图弄清楚如何混合自动布局和动画。

我有容器视图,ViewA,这是一定的高度。我有一个UIDatePicker,ViewB,它高216,并且有一个约束,用于设置ViewA和ViewB = 0之间的垂直空间,因此ViewB基本上固定在ViewA的底部。

enter image description here

我希望为ViewA的高度设置动画,使其更小,并使ViewB始终与ViewA的底部挂钩。

[UIView animateWithDuration:2.0 animations:^{
    CGRect containerFrame = self.tableContainerView.frame;
    containerFrame.size.height -= self.datePicker.frame.size.height;
    self.tableContainerView.frame = containerFrame;

} completion:^(BOOL finished) {
    NSLog(@"Done");
}];

容器视图ViewA确实会按预期缩短,但ViewB不会跟随。

如何在ViewA和ViewB之间获得约束以在动画期间自动调整ViewB的位置?

1 个答案:

答案 0 :(得分:3)

从你的约束看起来它应该有效。问题可能在于您制作动画的方式。你应该用约束而不是帧来做到这一点。如果viewA对superview的底部有约束,那么你可以只为该约束的常量设置动画。将IBOutlet设置为该底部约束,并执行此操作(bottomCon是我示例中的插座):

self.bottomCon.constant = self.datePicker.bounds.size.height;
[UIView animateWithDuration:2.0 animations:^{
    [self.view layoutSubviews];
    [self.containerView layoutSubviews]; 
} completion:^(BOOL finished) {
    NSLog(@"Done");
}];

在我的编辑中,我还添加了[self.containerView layoutSubviews]行。这是必要的,以便它的子视图也可以正确更新(否则它们只是跳转到新位置)。