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

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

问题描述

这是一种我面对现在的怪异,我知道,在一个自动版式IB添加约束很简单。但我真的无法弄清楚,为什么当我调整(ZoomIn动画)一个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。
当我拍了拍一个黄色的盒子,它应该zoomIn,如显示在它下面的照片是什么。

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.

我把一个前置,topSpace和bottomSpace从黄框内绿箱了。
我的code调整大小的黄色框为:

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.

从故事板,你可以简单的<一个访问限制href=\"http://stackoverflow.com/questions/21748156/change-constraints-based-on-device-during-run-time/21748704#21748704\">create IBOutlets 了解他们。

To access constraints from storyboard you can simply create IBOutlets for them.

[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的超级视图您试图动画视图(或更高)。

我建议在苹果的<一个关于自动版面阅读更href=\"https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AutolayoutPG/index.html#//apple_ref/doc/uid/TP40010853-CH7-SW1\"相对=nofollow>官方指南。

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

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