Problems with doing a flip animation between a UIView and UIScrollView - iphone

I'm new to iPhone development and I'm trying to do a flip animation between a UIView and another UIView containing a regular UIView and a UIScrollView, the scroll view in turn has several UIViews as subviews.
Before the animation start, the scroll view needs to be offset to a particular point to show a particular subview (the user is jumping to a particular "chapter" in the scroll view).
It is animating fine, but the problem is that it will 'sometimes' (every third time perhaps) start the animation with one of the subviews of the scroll view instead of starting the animation with the original UIView ('overlayView' in the code below). I suspect this has to do with the fact that I set the offset of the scroll view before animating.
This is how I currently do it:
// get the MPMoviePlayer window that has the views to animate between as subviews
UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
// tell the controller of the scroll view to set the scroll view offset
[instructionControlsController setInstructionImageWithNumber:[self chapterAtTime:currentTime]];
// Animate transition to instruction view
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView: moviePlayerWindow cache:NO];
// the views to animate between
[moviePlayerWindow sendSubviewToBack: overlayView];
[moviePlayerWindow bringSubviewToFront: instructionControlsController.view];
[UIView commitAnimations];
The setInstructionImageWithNumber method in the controller looks like this:
- (void) setInstructionImageWithNumber:(int)number
{
if (number < kNumberOfPages)
[scrollView setContentOffset: CGPointMake((number * kImageWidth), 0) animated:NO];
}
Any ideas of what I might be doing wrong and why I get this behavior where the animation looks fine sometimes and sometimes not?

What happens if you give the run loop a chance to update your view before beginAnimations? You might need to do this to give the view a chance to "catch up" and be accurately updated before animations start.
UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
[instructionControlsController setInstructionImageWithNumber:[self chapterAtTime:currentTime]];
//Before continuing the animation, let the views update
[self performSelector:#selector(continueTheAnimation:) withObject:nil afterDelay:0.0];
. . .
- (void)continueTheAnimation:(void*)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView: moviePlayerWindow cache:NO];
[moviePlayerWindow sendSubviewToBack: overlayView];
[moviePlayerWindow bringSubviewToFront: instructionControlsController.view];
[UIView commitAnimations];
}

Related

Animate a UIView change where the UIView is totally rebuilt

My UIViewController gets the view it is controlling to completely rebuild itself with something like this:
[self.view rebuildYourself:bIsLandscape]; // this line is in the ViewController
The view itself then contains the following method:
-(void)rebuildYourself:(BOOL)isLandscape
{
NSArray *viewsToRemove = [self subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
[self addControls]; // adds lots of views
[self layoutControlsWithOrientation:isLandscape]; // frames the views
}
I would like to animate the entire transition. I have tried this:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self.view rebuildYourself:bIsLandscape];
}
completion:^(BOOL finished){
}];
but the animation ignores the options value of UIViewAnimationOptionTransitionCurlUp and flows in from the top left corner every time.
and I have tried this:
[UIView beginAnimations:#"kkk" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView setAnimationDuration:1.0];
[self.view rebuildYourself:bIsLandscape];
[UIView commitAnimations];
but in this case the transition does curl up nicely but the underlying view is blank until the transition has finished, and then the new view suddenly 'pops' into view, which spoils the effect of the transition.
Can anyone tell me the right/best way to animate a transition where the view rebuilds itself completely?
Thanks
Make two views and put the old view overtop of the new re-built view. Then 'Curl-up' the old view so that the new view is showing and remove the old view from memory.

Switching views with animation in iPhone

I'm trying to create a simple animation when switching from a view to another in iPhone.
I have a controller with its view. In the same xib file I placed another view I called viewTracks. I would like to write an action that switches from the default view of the controller to the view viewTracks. This is the code of the action I wrote in my controller:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[self view] cache:YES];
UIView* sup = [[self view] superview];
[self.view removeFromSuperview];
[sup addSubview:viewTracks];
[UIView commitAnimations];
All I see is that the view does switch, but without any animation. Is there any mistake here? Why do I see the correct switch of view without an the animation I specified?
Thanks!
I would recommend making the the two views you want to switch between be subviews of your view controller's view.

Dismiss modalviewcontroller with a page curl

I am trying to dismiss a modalviewcontroller with a page curl. The curl works okay but I cannot seem to get the tableview under the modalviewcontroller to show up. The image of the modalviewcontroller is still under the curled away page. If I dismiss the modalviewcontoller before the animation finishes the animation doesn't show up. Here is my code:
//hide splash screen
- (void)hideSplash{
[UIView beginAnimations:nil context:nil];
//change to set the time
[UIView setAnimationDuration:2];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:modelView cache:NO];
// do your view swapping here
//[[self modalViewController] dismissModalViewControllerAnimated:NO];
[UIView commitAnimations];
//[self.view sendSubviewToBack:self.view];
}
Hope someone can help! Cheers Nick
In iOS4:
To present, it's something like:
[containerView addSubview:modelView];
[UIView transitionWithView:containerView
duration:.75
UIViewAnimationOptionTransitionCurlUp
animations:^{}
completion:^(BOOL finished) {
NSLog(#"finished %d", finished);
}];
To dismiss, use UIViewAnimationOptionTransitionCurlDown.
Your setAnimationTransition: shouldn't be forView:modelView; it should be for the parentView.
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:containerView cache:NO];
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html
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:
Begin an animation block.
Set the
transition on the container view.
Remove the subview from the container
view.
Add the new subview to the
container view.
Commit the animation
block.
Use of this method is
discouraged in iOS 4.0 and later. You
should use the block-based animation
methods instead.

Flip Animation not working

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.
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:
Begin an animation block.
Set the transition on the container view.
Remove the subview from the container view.
Add the new subview to the container view.
Commit the animation block.
So it says you have to create a container view in order to make it work properly.
The reason you're not getting the curl transition to work is because the curl transition does not work in the simulator. It shows up as a fade instead.

Flip View Iphone

please consider the code below, and tell me what I'm doing wrong.
I want to flip between two UIViews.
Somehow, when I flip away from the initial view, I just get the flipped view, without animation. When I flip back, the animation shows just fine.
The flips are triggered from buttons on the views themselves.
- (IBAction)showMoreInfo:(id)sender
{
UIView *moreInfo = self.flipView;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
UIView *parent = self.view.superview;
[self.view removeFromSuperview];
[parent addSubview:moreInfo];
[UIView commitAnimations];
}
- (IBAction)showLessInfo:(id)sender
{
UIView *lessInfo = self.view;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.flipView cache:YES];
UIView *parent = self.flipView.superview;
[self.flipView removeFromSuperview];
[parent addSubview:lessInfo];
[UIView commitAnimations];
}
It's probably because you aren't using a container view as the transition view. Refer to the documentation on setAnimationTransition:forView:cache:
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:
Begin an animation block.
Set the transition on the container view.
Remove the subview from the container view.
Add the new subview to the container view.
Commit the animation block.
Try using self.view.superview in the animation transition view of the showMoreInfo:
The reason the showLessInfo: method works is you are using a container view.
Can you use your MainWindow (UIWindow) as the container view as UIWindow inherence from UIView?
Also iPhone 3.0 introduced the flip transaction via the presentModalViewController method:
CustomViewController *vc = [[CustomViewController alloc]
initWithNibName:#"CustomViewController" bundle:nil];
vc.delegate = self;
// The magic statement. This will flip from right to left.
// present the modal view controller then when you dismissModalViewController
// it will transition flip from left to right. Simple and elegant.
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:vc animated:YES];
[vc release];
After iOS 4.0, you can flip between views with this:
[UIView transitionFromView:sourceView toView:destinationView duration:0.35f options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
NSLog(#"I just flipped!");
}];
As Jason mentioned, you'll need this to occur within a container view.