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:
- I am loading this customView from a
nib
that is specifically 310 by 150, perhaps that is causing a problem. - 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
andCore 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!
Try this:
Or if you want to use beginAnimation method instead of block Methos , Use this :