动画视图宽度,同时使用自动布局 [英] Animating view width whilst using auto layout

查看:127
本文介绍了动画视图宽度,同时使用自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自动布局来定位一个自定义的进度条(其简单地用背景色一个UIView)。我不是使用适当的进度条,因为我没有想必须把它完全定制,以我的需求,当一个UIView就足够了。

反正我已经告诉过X code到放置在进度条到左边,并从底部一定的距离。这种正确的定位,但是当我动画的宽度没有任何反应。如果我创建一个新的UIView,然后动画能够正常工作。是制约$ P $从动画pventing呢?没有约束在所有放置在宽度

  [UIView的animateWithDuration:startingGameSeconds延迟:0.0选项:UIViewAnimationOptionCurveLinear
                 动画:^(无效){
                     progressBar.frame = CGRectMake(progressBar.frame.origin.x,
                                                    progressBar.frame.origin.y,
                                                    320,
                                                    progressBar.frame.size.height);                 }
                 完成:^(BOOL完){
                 }];


解决方案

当你使用自动布局,你不应该使用被SETFRAME:方法了。当你想进行动画处理的框架,你必须动画的约束。我想你的自动布局约束在Interface Builder中定义。首先,确保你有添加到您的进度条的宽度约束布局。接下来,这个宽度布局约束创建一个IBOutlet。现在,您可以动画约束是这样的:

  [UIView的animateWithDuration:1.F动画:^ {
   [自progressBarWidthLayoutConstraint] setConstant:320.f];
   [自拍] layoutIfNeeded]
}];

I am using auto layout to position a custom progress bar (its simply a UIView with a background colour). Im not using a proper progress bar because I didnt want to have to fully customise it to my needs when a UIView will suffice.

Anyway I have told XCode to place the progress bar to the left, and a certain distance from the bottom. This positions it correctly but when I animate the width nothing happens. If I create a new UIVIew then animate that it works correctly. Are the constraints preventing this from animating? No constrains is placed upon the width at all

[UIView animateWithDuration:startingGameSeconds delay:0.0 options:UIViewAnimationOptionCurveLinear
                 animations:^(void) {
                     progressBar.frame = CGRectMake(progressBar.frame.origin.x,
                                                    progressBar.frame.origin.y,
                                                    320,
                                                    progressBar.frame.size.height);

                 }
                 completion:^(BOOL finished){
                 }];

解决方案

When you're using Auto Layout you shouldn't be using the setFrame: method anymore. When you want to animate the 'frame', you'll have to animate the constraints. I suppose your Auto Layout constraints are defined in Interface Builder. First, make sure you have a width layout constraint added to your progress bar. Next, create an IBOutlet for this width layout constraint. You can now animate the constraint like this:

[UIView animateWithDuration:1.f animations:^{
   [[self progressBarWidthLayoutConstraint] setConstant:320.f];
   [[self view] layoutIfNeeded];
}];

这篇关于动画视图宽度,同时使用自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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