Objective-c – Flip Animation not working

animationiphoneobjective cuiviewxcode

I have a Custom view. This custom view has two UIImageViews – imageview1 and imageview2.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.00];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(transitionDidStop:finished:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];
if(frontVisible) {
[imageview1 removeFromSuperview];
[self addSubview: imageview2];
}
else {
[imageview2 removeFromSuperview];
[self addSubview: imageview1];
}
frontVisible = !frontVisible;
[UIView commitAnimations];


The image changes from imageview1 to imageview2 and viceversa but I dont get the flip effect. Instead I see the fading out of one image as the other appears.

Best Answer

Not really sure but I checked the documentation and found this:

Discussion

If you want to change the appearance of a view during a transition—for example, flip from one view to another—then use a container view, an instance of UIView, as follows:

  1. Begin an animation block.
  2. Set the transition on the container view.
  3. Remove the subview from the container view.
  4. Add the new subview to the container view.
  5. Commit the animation block.

So it says you have to create a container view in order to make it work properly.