Chris Roberts Chris Roberts - 3 months ago 15
iOS Question

iOS: UIView animation, no animating happening

Having a puzzling problem. I have a universal app with a lot of shared code between the iPad and iPhone versions. There are different layouts in the nibs but essentially the same views and view hierarchy - one UIView used as a container for two sibling UITextViews.

UIView mainView with children:

UITextView passageTextView

UITextView notesTextView

One UITextView is hidden, the other visible.

The following is my code. The section commented out was my original animation attempt. This worked just as desired on the iPad, but not on the iPhone. The uncommented section is take 2, using the method recommended in the docs. The uncommented code does not work on either the iPad or iPhone - it hides/unhides my views but without any animation. If I add code to the completion block that also gets executed, so it's doing something, just not animating.

/*
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:mainView cache:YES];

passageTextView.hidden = YES;
notesTextView.hidden = NO;

[UIView commitAnimations];
*/

UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationTransitionFlipFromRight;

[UIView transitionWithView:mainView
duration:1.0
options:options
animations:^{ passageTextView.hidden = YES; notesTextView.hidden = NO; }
completion:NULL];


Edit: Still working on the problem, hoping someone has a suggestion.

Additional update

Figured out why the following was not working in the iPhone:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:mainView cache:YES];

passageTextView.hidden = YES;
notesTextView.hidden = NO;

[UIView commitAnimations];


I had neglected to wire the view to mainView in Interface Builder. Hours debugging and I just now thought to check that.

But, I still do not know why animation blocks are not working for either the iPhone or iPad. I have tried several approaches but I'm not getting any animation even though the show/hides are working.

Answer

I think you are using the wrong animation option.

Replace your second animation option by UIViewAnimationOptionTransitionFlipFromLeft (note the Option between Animation and Transition)

I believe that UIViewAnimationTransitionFlipFromLeft (which is what you have in your code) is a UIViewAnimationTransition not a UIViewAnimationOptions.