slide swapping animation

Discussion in 'iOS Programming' started by chhoda, May 7, 2009.

  1. chhoda macrumors 6502

    Oct 25, 2008
    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

  2. BlackWolf macrumors regular

    Apr 9, 2009
    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?
  3. chhoda thread starter macrumors 6502

    Oct 25, 2008

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

  4. BlackWolf macrumors regular

    Apr 9, 2009
    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.

Share This Page