在通过动画调整UIView大小时,约束不起作用 [英] Constraints not working when Resizing UIView via Animation

查看:303
本文介绍了在通过动画调整UIView大小时,约束不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我面临的奇怪的事情,我知道在AutoLayout IB中添加约束很简单。但我真的不知道为什么我的简单约束不工作,当我调整大小(缩放动画)UIView。

It's kind the weird I'm facing right now, I know that adding constraints in an AutoLayout IB is simple. But I really couldn't figure-out why my simple constraints are not working when I'm resizing (ZoomIn Animation) a UIView.

我定位到zoomIn的视图工作正常,但其中的子视图未定位得很好。看起来这些约束不起作用。

The View I'm targeting to zoomIn works fine, but the subviews within it is not being positioned well. It seems that the constraints are not working.

在这个设置中,黄色框中的是UIViews,绿色框是UIImageViews。
当我点击黄色框,它应该放大,如下面的照片所示。

From this setup, where in the Yellow Boxes are UIViews and the Green Boxes are UIImageViews. When I tapped on a Yellow Box, it should zoomIn, as what is shown on the photo below it.

这些是约束:

These are the constraints:

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

This should be the expected resulting upon zoomIn AnimationBut I got no luck, The yellow box zoomIn but the Green Box is let on its old position and did not changed size.

我把一个领先的, ,和bottomSpace从黄箱到绿箱已经。
我的代码调整黄色框的大小是:

I put a leading, trailing, topSpace, and bottomSpace from Yellow Box to Green Box already. My code for resizing the yellow box is:

- (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];
}

这应该是zoomIn动画的预期结果:

This should be the expected resulting upon zoomIn Animation:

推荐答案

当尝试使用自动版式制作动画时,您应该直接更改视图的帧。

When trying to animate with Auto Layout, you should not change view's frames directly. Instead, you should animate changes in constraints.

要从故事板访问约束,您可以简单地为他们创建IBOutlets

[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]; 
}];

注意,你通常想要调用layoutIfNeeded在超级视图

我建议您阅读Apple的官方指南

I recommend reading more on Auto Layout in Apple's official guide.

这篇关于在通过动画调整UIView大小时,约束不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆