PDA

View Full Version : slide swapping animation




chhoda
May 7, 2009, 03:48 AM
Hi All,
I am asked to do a slide swapping animation kind of stuff. Are there any examples ? is it possible using core animation ? Actually in may main view i will have 2 subviews And i want to swap their positions with an animation on touch drag.

any pointers are welcome

--CH



BlackWolf
May 7, 2009, 05:24 AM
so you just want to exchange the position of the two subviews, did I get that right? if so, that can be done even without Core Animation. UIView has some animation methods that allow you to animate changes to the frame and alpha and some other values of the view. see the docs of UIView for that.
basically, you start the animation block with "beginAnimations:context:", then you can set some animation properties with stuff like "setAnimationDuration:", then you simply tell the animation what you want to change, for example
subview1.frame = newPositionRect;
and then you fire the animation with "commitAnimations"

was that even what you wanted to know? :D

PS: afaik, this is also possible with core animation, but if you don't need it why make things complicated?

chhoda
May 8, 2009, 01:53 AM
thanks for replying, yes thats what exactly i wanted, following piece of code worked. thanks for giving the ideas.

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:1];
CGRect btnOneFrame = [self.buttonOne frame];
CGRect btnTwoFrame = [self.buttonTwo frame];
[self.buttonOne setFrame:btnTwoFrame];
[self.buttonTwo setFrame:btnOneFrame];

[UIView commitAnimations];

-CH

BlackWolf
May 8, 2009, 06:06 AM
note that it's getting kinda complicated when it is possible that an animation starts while an animation is running.
UIView has an "startAnimationFromCurrentState" property, but it never worked for me when I animated the position of a view. so you should make sure that two animations are not running at the same time on the same view.