Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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]; 

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]; 

But it still doesn't work!

EDIT:

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. 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.

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!

like image 364
Hudson Buddy Avatar asked Oct 12 '12 04:10

Hudson Buddy


People also ask

Does UIView animate need weak self?

You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.

How do I change the view height in Swift?

1) Control-drag from a frame view (e.g. questionFrame) to main View, in the pop-up select "Equal heights". 2)Then go to size inspector of the frame, click edit "Equal height to Superview" constraint, set the multiplier to 0.7 and hit return.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.


2 Answers

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.

like image 61
jrturton Avatar answered Sep 24 '22 08:09

jrturton


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!" );  }]; 
like image 30
Alec Avatar answered Sep 23 '22 08:09

Alec