iOS UIView animation : how to animate both position and size

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
Hi,

I want to change the position of my view, and at the same time, shrink it.
The problem is when i use animation, it seems that the size is changed first to the new size, and then the position is animated.

Anyone know why it is and how to solve this ? Thanks a lot.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,999
306
Harrogate
Did you read the documentation?

The Excellent UIView Documentation said:
Some of the property changes to view objects can be animated—for example, setting the frame, bounds, center, and transform properties. If you change these properties in an animation block, the changes from the current state to the new state are animated. Invoke the beginAnimations:context: class method to begin an animation block, set the properties you want animated, and then invoke the commitAnimations class method to end an animation block. The animations are run in a separate thread and begin when the application returns to the run loop. Other animation class methods allow you to control the start time, duration, delay, and curve of the animations within the block.
 

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
I read it but I still dont get why my code doesn't work as expected. I set a new frame to my view, and put it inside an animation block. So why it just animates the position but not the size ?
 

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
Sorry i forgot. This is my code :

Code:
[UIView beginAnimations : @"Display notif" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationBeginsFromCurrentState:FALSE];

CGRect frame = mainView.frame;
frame.size.height -= 40;
frame.origin.y += 40;
mainView.frame = frame;
		
[UIView commitAnimations];
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,999
306
Harrogate
Is there a transform involved? As the documents state:

"If the transform property is also set, use the bounds and center properties instead; otherwise, animating changes to the frame property does not correctly reflect the actual location of the view."

Have you tried setting the bounds and centre properties instead of the frame?
 

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
I dont use any transform. I've tried setting the bounds and center instead, but it produced the same effect.

The code is here :

Code:
[UIView beginAnimations : @"Display notif" context:nil];
	[UIView setAnimationDuration:1];
	[UIView setAnimationBeginsFromCurrentState:FALSE];

	CGPoint center = mainView.center;
	center.y += 20;
	mainView.center = center;
	
	CGRect bounds = mainView.bounds;
	bounds.size.height -= 40;
	mainView.bounds = bounds;
	
	[UIView commitAnimations];
 

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
Does it matter ? It's called inside a UIViewController, and triggered by a button. When user tap the button, this code will be called.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
It might matter. There can be problems is more than one animation is happening at a time or if one animation is nested inside another.

If the code is run in response to a touch event from a button then that probably doesn't apply.
 

ranguvar

macrumors 6502
Sep 18, 2009
318
1
Code:
[UIView beginAnimations : @"Display notif" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationBeginsFromCurrentState:FALSE];

CGRect frame = mainView.frame;
frame.size.height -= 40[B][COLOR="Red"].0[/COLOR][/B];
frame.origin.y += 40[B][COLOR="Red"].0[/COLOR][/B];
mainView.frame = frame;
		
[UIView commitAnimations];
Does it work now? (Remember frame.size.height and frame.origin.y are CGFloat values.)
 

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
Thanks for all your replies. I have found the may-be problem.

Above i animated the mainView, which has 1 subview. This subview has the same size as mainView, and its position is (0,0) . The autoresizingMask of the subview is set to flexible width and flexible height.

When i animate mainView, it seems that the animation on mainView is correct, but the animation on the subview is not what i expect. The subview is firstly resized to the new size, and then its position is animated. But because i only see the subview, so i thought that there's something wrong with the animation of mainView.

So now my question is, how can i animate the subview ? Thanks.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.