PDA

View Full Version : UIView animation : how to animate both position and size




namanhams
Jul 27, 2010, 04:18 AM
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
Jul 27, 2010, 04:45 AM
Did you read the documentation (http://developer.apple.com/iphone/library/documentation/uikit/reference/UIView_Class/UIView/UIView.html)?


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
Jul 28, 2010, 04:48 AM
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 ?

robbieduncan
Jul 28, 2010, 05:05 AM
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 ?

If you don't post your code how can we answer?

namanhams
Jul 28, 2010, 05:20 AM
Sorry i forgot. This is my 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
Jul 28, 2010, 05:29 AM
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
Jul 28, 2010, 10:44 PM
I dont use any transform. I've tried setting the bounds and center instead, but it produced the same effect.

The code is here :


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

PhoneyDeveloper
Jul 29, 2010, 12:25 AM
When is this code called?

namanhams
Jul 29, 2010, 12:44 AM
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
Jul 29, 2010, 12:54 AM
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
Jul 29, 2010, 02:08 AM
[UIView beginAnimations : @"Display notif" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationBeginsFromCurrentState:FALSE];

CGRect frame = mainView.frame;
frame.size.height -= 40.0;
frame.origin.y += 40.0;
mainView.frame = frame;

[UIView commitAnimations];


Does it work now? (Remember frame.size.height and frame.origin.y are CGFloat values.)

dejo
Jul 29, 2010, 10:56 AM
[UIView setAnimationBeginsFromCurrentState:FALSE];
Although it doesn't make much difference, the common convention is to set BOOL using YES or NO.

namanhams
Jul 29, 2010, 11:39 PM
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.

robbieduncan
Jul 30, 2010, 04:19 AM
I'd try setting the autoresizesSubviews (http://developer.apple.com/iphone/library/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/autoresizesSubviews) property to no on the outerview and adding the innerview sizing to the animation block you already have.