2
votes

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.

4
Michieal, I am facing a similar problem. When I try to animate a view to bigger size both with its origin and width / height changed, the view is re-sized immediately and then animated to its new location. It is similar to what you have quoted but occurs when I try to blow up the view. The code is just same as what you have posted above. What might be the reason for this problem? How did you solve it? For posterity and future references please suggest your answer so that others might benefit. - Raj Pawan Gumdal

4 Answers

3
votes

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

0
votes

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

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:

UIView scaling during animation

0
votes

I worked around this problem when releasing my app by just removing the animation. It kept bugging me in the back of my mind, so just this week I thought that maybe by using the center property and the size instead of the origin it would make a difference.

So I decided to test this out. Guess what. The problem doesn't occur when using sdk 4.x

Thanks all for trying to help me out.