简体   繁体   English

使用约束动画UIView框架

[英]Animating UIView frame with constraints

I have an element in a UIView with a constraint that says it should always be 10 pixels from the bottom of the view. 我在UIView中有一个元素,其约束条件是它应该始终距离视图底部10个像素。 I am then attempting to animate this view's height so that it appears to slide down the screen. 然后我尝试为此视图的高度设置动画,使其看起来在屏幕上滑动。 According to the constraint, the element should always be 10 pixels from the bottom of the view. 根据约束,元素应始终距视图底部10个像素。 This holds true when I add the view like so... 当我像这样添加视图时,这是正确的...

printView *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"printView"];
    [self addChildViewController:vc];
    vc.view.frame = CGRectMake(0, 60, vc.view.frame.size.width, HEIGHT);
    vc.view.layer.masksToBounds = FALSE;
    [vc.view.layer setShadowOffset:CGSizeMake(0.0, 5.0)];
    [vc.view.layer setShadowOpacity:0.8];
    vc.view.layer.shadowColor = [[UIColor blackColor] CGColor];
    vc.view.layer.shadowRadius = 8;
    vc.view.clipsToBounds = TRUE;
    [self.view insertSubview:vc.view aboveSubview:docWebView];

I can change HEIGHT to whatever I want and the element is always 10 pixels from the bottom of the view. 我可以将HEIGHT更改为我想要的任何内容,并且元素总是距离视图底部10个像素。 The problem comes when I try to animate the height 当我尝试设置高度动画时出现问题

printView *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"printView"];
    [self addChildViewController:vc];
    vc.view.frame = CGRectMake(0, 60, vc.view.frame.size.width, 0);
    vc.view.layer.masksToBounds = FALSE;
    [vc.view.layer setShadowOffset:CGSizeMake(0.0, 5.0)];
    [vc.view.layer setShadowOpacity:0.8];
    vc.view.layer.shadowColor = [[UIColor blackColor] CGColor];
    vc.view.layer.shadowRadius = 8;
    vc.view.clipsToBounds = TRUE;
    [self.view insertSubview:vc.view aboveSubview:docWebView];
    [UIView animateWithDuration:5.0 animations:^{
        vc.view.frame = CGRectMake(0, 60, vc.view.frame.size.width, 200);
        [self.view setNeedsDisplay];
    }];

The constraint is no longer honored and instead of the view sliding in with the element always 10 pixels from the bottom, it looks like the element is being uncovered because the element does not move with the view. 约束不再受到尊重,而不是视图滑动,元素始终距离底部10个像素,看起来元素正在被覆盖,因为元素不随视图移动。 I hope I am explaining this well enough. 我希望我能够很好地解释这一点。 To put it another way, I'm going for the effect of a map being pulled down, but instead it looks like the map is already there and a piece of paper that is covering it is being pulled away. 换句话说,我正在寻找地图被拉下来的效果,但看起来地图已经在那里,一张覆盖它的纸被拉开了。 Thanks for your help. 谢谢你的帮助。

When using constraints, you shouldn't be setting frames, you should be adjusting the constraints. 使用约束时,不应该设置框架,应该调整约束。 When you initially set up your constraints, you should make an IBOutlet to the height constraint, and then animate its constant parameter in the animation block. 初始设置约束时,应将IBOutlet设置为高度约束,然后在动画块中设置其常量参数的动画。 If you had a height constraint called heightCon, you could do this: 如果你有一个名为heightCon的高度约束,你可以这样做:

[UIView animateWithDuration:5.0 animations:^{
        self.heightCon.constant = 200
        [self.view layoutIfNeeded];
    }];

I'm not sure about your structure, that self.view might have to be vc.view instead. 我不确定你的结构,self.view可能必须是vc.view。

After Edit: 编辑后:

This is the way to animate a height constraint, but I'm not sure that's what you want to do to accomplish the look you're after. 这是为高度约束设置动画的方法,但我不确定你想要做什么来完成你所追求的外观。 I'm not really sure what to make of your last paragraph. 我不确定你的最后一段是怎么做的。 If you're going for an effect of a map being pulled down, it seems like the bottom constraint needs to be either animated or eliminated. 如果您想要拉下地图的效果,则底部约束似乎需要动画或消除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM