How to animate the width and height of a UIView in Xcode? How to animate the width and height of a UIView in Xcode? xcode xcode

How to animate the width and height of a UIView in Xcode?


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

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.

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.

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.


Try using a block it will be more clear.

 // 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!" ); }];


Try this:

customView.frame= CGRectMake(self.view.frame.origin.x +5,self.view.frame.origin.y+5,0, 150);[self.view addSubview:customView];CGRect frame = customView.frame;frame.size.width = 310;[UIView animateWithDuration:0.4 animations:^{  customView.frame= frame;  }];

Or if you want to use beginAnimation method instead of block Methos , Use this :

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