如何在Xcode中为UIView的宽度和高度设置动画?

Hud*_*ddy 22 xcode animation uiview uiviewanimation ios

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

[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];
Run Code Online (Sandbox Code Playgroud)

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

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];
Run Code Online (Sandbox Code Playgroud)

但它仍然不起作用!

编辑:

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

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) {

                 }];
Run Code Online (Sandbox Code Playgroud)

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

  1. 我正在加载这个特别nib是310乘150的customView ,这可能会导致问题.
  2. 我没有导入正确的框架,但由于我可以为此视图的frame.origin部分设置动画,我不确定是这种情况......我已经QuartzCoreCore Graphics所有导入的东西.

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

jrt*_*ton 50

要使视图"增长"到位,请不要为框架设置动画.动画变换.

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

view.transform = CGAffineTransformMakeScale(0,0);
Run Code Online (Sandbox Code Playgroud)

然后将其添加到您的superview.

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

view.transform = CGAffineTransformIdentity;
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 你绝对可以为框架设置动画.可能的解释是,您可能没有将clipsToBounds设置为TRUE,这就是为什么您仍然看到最终帧而不是剪切帧? (2认同)

Ale*_*lec 8

尝试使用一个块会更清楚.

 // First create the view if you haven't already 
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; 
// Add it as a subview. 
[myViewController.view addSubview:myView]; 


CGRect newFrameOfMyView = CGRectMake(0.0f, 0.0f, 200.0f, 200.0f); 
/*
 * Alternatively you could just set the width/height or whatever you'd like with this: 
 * CGRect newFrameOfMyView = myView.frame; 
 * newFrameOfMyView.size.height = 200.0f; 
 * newFrameOfMyView.size.width = 200.0f; 
 */
[UIView animateWithDuration:0.3f
     animations:^{
      myView.frame = newFrameOfMyView; 
     }
     completion:^(BOOL finished){ 
     NSLog( @"woo! Finished animating the frame of myView!" ); 
}];
Run Code Online (Sandbox Code Playgroud)