0

I have one view controller which contains two views (redView, blueView). The views are smaller than the main view.

I want to change from redView to blueView with animation. If use this none animation happens.

[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight 
                       forView:redView cache:YES]; // !!! I WILL CHANGE THIS

[self.view addSubview:blueView];
[redView removeFromSuperview];

[UIView commitAnimations];

In case I change the code to this, then the animation is ok but the whole mainView animates, something that i do not want to happen. I want only the subViews to flip. I should note that the 2 subviews are in the same position. (frame) Any ideas?

[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight 
                       forView:self.view cache:YES];

[self.view addSubview:blueView];
[redView removeFromSuperview];

[UIView commitAnimations];

3 Answers 3

4

Use Block animation instead of simple animation, from apple about simple animation "Use of the methods in this section is discouraged in iOS 4 and later"

Animation With Block's

[UIView transitionWithView:containerView
                  duration:1.25
                   options:UIViewAnimationOptionTransitionFlipFromRight
                animations:^
{ 

                    [redView removeFromSuperview]; 
                    [containerView  addSubview:blueView];

                }
                completion:NULL];

containerView : is main view that will be animated. Also add to your containerView redView, that all :) for more info look at

http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html

0

Solution Found:

I use a tempView for which i do the animation. Onto the tempView I add view1 and view2.

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:tempView cache:YES];
[tempView addSubview:view2]; 
[view1 removeFromSuperview];
[UIView commitAnimations];

Extra: For a better animation you can use view1.visible = YES , NO instead of addSubview, removeFromSuperView, but they will be allocated all the time

0

In your first example it looks like you are deleting the redView before it can animate, try deleting it after the animation.

Code:

[UIView setAnimationDidStopSelector:@selector(removeRedViewFromSuperview)];
2
  • Thanks for your help but it did not work. I found out the solution. [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:tempView cache:YES]; [tempView addSubview:view2]; [view1 removeFromSuperview]; [UIView commitAnimations];
    – gsach
    Dec 22, 2011 at 15:36
  • I cannot post it yet because of my reputation
    – gsach
    Dec 22, 2011 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.