如何在Xcode中为UIView的宽度和高度设置动画? [英] How to animate the width and height of a UIView in Xcode?

查看:157
本文介绍了如何在Xcode中为UIView的宽度和高度设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将这个子视图添加到我的主视图中,但是要从原点扩展它。我阅读了一些Apple文档,但我不明白我在哪里弄错了。我可以为框架的原点设置动画,即让它从任何地方滑入,但宽度/高度似乎没有动画。代码如下:

I have this subview I want to add to my main view, but make it expand from the origin. I read some of the Apple documentation but I don't understand where I am making mistake. I can animate the origin of the frame, i.e. to get it slide in from wherever, but the width/height doesn't seem to animate. Code as follows:

[UIView beginAnimations:@"animateAddContentView" context:nil];
[UIView setAnimationDuration:0.4];
customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 0, 150);
[self.view addSubview:customView];

customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 310, 150);
[UIView commitAnimations];

我也试过在动画中只放置setFrame部分,如下所示:

I have also tried putting only the setFrame part in the animation like this:

customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 0, 150);
[self.view addSubview:customView];
[UIView beginAnimations:@"animateAddContentView" context:nil];
[UIView setAnimationDuration:0.4];
customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 310, 150);
[UIView commitAnimations];

但它仍然不起作用!

编辑:

根据建议,我已将其移至基于块的动画解决方案,是确切的代码:

As per the suggestions, I have moved it to a block based animation solution, and this is the exact code:

NSLog(@"yourview : %@",customView);
customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 0, 150);

NSLog(@"yourview : %@",customView);
[self.view addSubview:customView];
NSLog(@"yourview : %@",customView);

//    [customView setFrame:CGRectMake( 0.0f, 480.0f, customView.frame.size.width, customView.frame.size.height)];

[UIView animateWithDuration:0.4
                      delay:0
                    options:UIViewAnimationCurveEaseInOut
                 animations:^ {
                     NSLog(@"yourview : %@",customView);

                     customView.frame = CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 310, 150);
                     NSLog(@"yourview : %@",customView);

                 }completion:^(BOOL finished) {

                 }];

由于某种原因,这仍然不起作用。我看到的可能问题是:

Which still doesn't work for some reason. Possible problems I see are:


  1. 我从 nib 加载此customView特别是310乘150,也许这会导致问题。

  2. 我没有导入正确的框架,但是由于我可以为这个视图的frame.origin部分设置动画,我不确定就是这样......我有 QuartzCore 核心图形所有导入的东西。

  1. I am loading this customView from a nib that is specifically 310 by 150, perhaps that is causing a problem.
  2. I am not importing the correct frameworks, but since I can animate the frame.origin parts of this view, I am not sure that is the case...I have QuartzCore and Core Graphics all imported and stuff.

在日志中的每一点都给出正确的东西:例如,在目标帧之前,大小为0,150,并且在我设置帧之后它的310,150。但动画不起作用!

At each point in the log it is giving the correct stuff: for example, before the target frame the size is 0, 150, and after I set the frame its 310, 150. But the animation doesn't work!

推荐答案

要使视图成长到位,不要为框架设置动画。动画变换。

To make a view "grow" into place, don't animate the frame. Animate the transform.

从笔尖加载子视图。将其变换设置为0:

Load your subview from the nib. Set its transform to a scale of 0:

view.transform = CGAffineTransformMakeScale(0,0);

然后将其添加到您的超级视图中。

Then add it to your superview.

在动画块中,将变换设置为identity:

Inside the animation block, set the transform to identity:

view.transform = CGAffineTransformIdentity;

视图将增长到正常大小。您可能需要摆弄锚点,使其从正确的点开始生长。

And the view will grow to normal size. You may need to fiddle with the anchor point to make it grow from the right point.

您也可以更改块内的帧,但仅移动视图我更喜欢更改中心属性,如果您不想尝试设置帧也有变化。

You can also change the frame within the block, but to move a view only I prefer to change the center property, you shouldn't try to set the frame if you also have a transform.

对于奖励积分,您还可以设置动画到略大于1.0的比例,然后在完成块的链式动画中动画回到身份转换。这使得视图像弹出视图一样弹出在屏幕之外。

For bonus points, you can also animate to a slightly bigger than 1.0 scale, then animate back to the identity transform in a chained animation from the completion block. This makes the view "pop" out of the screen like an alert view.

这篇关于如何在Xcode中为UIView的宽度和高度设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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