views:

635

answers:

3

Hi,

I'm trying to animate a UIView so that it can grow larger when tapping a button and shrinks down to its original size by tapping the button again.

I do this by adjusting frame to its new size and origin and running the code below.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.theView.frame = frame;
[UIView commitAnimations];

When animating to a bigger size the animation is how it should be. But when animating to its original size (smaller) the view is re-sized instantly and then animated to its designated location.

What I need is that the resizing to the smaller frame is animated as well.

I've tried adjusting the autoresizingMask as well, but that didn't change anything.

Hope someone can help.

thanks.

A: 

Try this

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationRepeatAutoreverses:YES];
self.theView.frame = frame;
[UIView commitAnimations];

In response to comment;

If you want to control animation reverse, have a flag like BOOL isbig and set it in app launch to NO, then toggle it in every button tap, then check it if it is YES or NO, and according to the result call a method. If NO, call your code, if not call the code again but this time set the frame to the original size.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.theView.frame = originalFrame;
[UIView commitAnimations];
EEE
This automatically reverses the animation. What i'm looking for is matching the expanding animation to the shrinking animation. Right now the shrinking animation resizes instantly to the smaller size instead of animating this.
Michiel
See the update, if I understand you correctly, this should be fine.
EEE
Thanks for your info.But this is what I do. based on my flag I call the animation with either bigFrame or smallFrame.The trouble is, when going to big, the animation is perfect. When going to small, the UIView framesize gets updated instantly and then the smaller frame is moved to its new coordinates. What I was expecting is something like the animation to big, where the frame size and the frame position would be updated gradually. This is not happening.for now I removed the animation. I just don't understand why the reversed animation is not exactly the same (but reverse).
Michiel
Michiel I just remembered that same problem was bothering me once. I was trying to resize an UIView like you said. I've solve the problem using UIImageView instead of UIVIew. I use it like button, I create a custom button, and UIImageView behind it, and it works perfect, trust me.
EEE
A: 

I could solve the problem by suggestion found in other thread, it has its own limitations though. I have posted the code in the same thread where the solution was suggested:

http://stackoverflow.com/questions/818207/uiview-scaling-during-animation/3202828#3202828

Raj
+2  A: 

Michiel, Raj,

I made a youtube tutorial showing how to make views expand and shrink like in the facebook iPhone app.

Hope it can be of some help: How to make expanding/shrinking views on iPhone SDK

Adam

Adam