通过动画调整UIView大小时,约束不起作用

Jos*_*gan 4 iphone constraints uiviewanimation ios autolayout

我现在面对的很奇怪,我知道在AutoLayout IB中添加约束很简单.但是当我调整(ZoomIn Animation)一个UIView时,我真的无法弄清楚为什么我的简单约束不起作用.

我瞄准zoomIn的视图工作正常,但其中的子视图定位不好.似乎约束不起作用.

从这个设置,黄色框中的UIViews和绿色框是UIImageViews.当我点击一个黄色的盒子时,它应该放大,如下面的照片所示.

在此输入图像描述

这些是约束:
在此输入图像描述

这应该是预期的结果zoomIn AnimationBut我没有运气,黄色框zoomIn但绿色框是让它的旧位置,并没有改变大小.

我已经将一个领先的,尾随的,顶级的,以及来自Yellow Box的bottomSpace放到了Green Box中.我调整黄色框大小的代码是:

- (void) resizeViewWithZoomInAnimation: (UIView*)view duration:(float)secs option:(UIViewAnimationOptions) option {

//sets the new width and height of deal's view
float newWidth = view.superview.frame.size.width - 20;
float newHeight = view.superview.frame.size.height - 20;

[UIView animateWithDuration:secs delay:0.0 options:option
                 animations: ^{

                     switch ([view tag]) {
                         case 1:
                             [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, newWidth, newHeight)];
                             break;
                         case 2:
                             [view setFrame:CGRectMake(CGRectGetMaxX(view.frame),CGRectGetMinY(view.frame), -newWidth , newHeight)];
                             break;
                         case 3:
                             [view setFrame:CGRectMake(CGRectGetMinX(view.frame),CGRectGetMaxY(view.frame), newWidth , -newHeight)];
                             break;
                         case 4:
                             [view setFrame:CGRectMake(CGRectGetMaxX(view.frame),CGRectGetMaxY(view.frame), -newWidth, -newHeight)];
                             break;
                     }
                 }
                 completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

这应该是zoomIn Animation的预期结果: 图片

Nik*_*kin 15

当试图用自动布局动画,你应该不会直接改变视图的帧.相反,您应该为约束中的更改设置动画.

要从故事板访问约束,您只需为它们创建IBOutlet即可.

[containerView layoutIfNeeded]; // complete pending layout operations (optional)
[UIView animateWithDuration:1.0 animations:^{

    // Update constraints
    self.viewConstraint1.constant = NEW_VALUE;
    self.viewConstraint2.constant = NEW_VALUE;

    // Animate the changes
    [containerView layoutIfNeeded]; 
}];
Run Code Online (Sandbox Code Playgroud)

请注意,您通常希望在要尝试设置动画的视图的超级视图(或更高)上调用layoutIfNeeded.

我建议您阅读Apple 官方指南中有关Auto Layout的更多信息.