Beginner: Multiview app doesn't switch views correctly - iphone

I wrote a basic two-view application that switches from one view to another every time I press a button.
But for some reason when I run it on simulator, both views are always few pixels above the MainWindow.xib view, always being on top of it. And what's strange is that there is no animation when I switch between Views.
What is the problem???
This is what I have in my AppDelegate.m
-(void)switchView:(UIView *)view1 toView:(UIView *)view2{
[UIView beginAnimations:#"Animation" context:nil];
[UIView setAnimationDuration:1.75];
[UIView setAnimationTransition:UIViewAnimationOptionTransitionFlipFromLeft forView:self.window cache:YES];
[view1 removeFromSuperview];
[window addSubview:view2];
[UIView commitAnimations];
}

Here are two very nice examples:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/13427-uiview-slide-transition.html
http://www.dizzey.com/development/ios/simple-uiview-transitions-animation-using-blocks-in-ios-4/
Or this one:
iOS 4.2: Flip image using block animations

Hi Try This,
-(void)switchView:(UIView *)view1 toView:(UIView *)view2
{
view2.frame = self.window.bounds;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view2 cache:NO];
[view1 removeFromSuperview];
[window addSubview:view2];
[UIView commitAnimations];
}
Also try this
[UIView transitionWithView:view2 duration:0.5
options:UIViewAnimationTransitionFlipFromLeft //change to whatever animation you like
animations:^ { [window addSubview:view2]; }
completion:nil];

Can you post some code regarding this .Because if you click on Segment change
it will change the Views.IF you take two views on same interface Builder.
Try out this
IBOutlet UIView *viewA;
IBOutlet UIView *viewB;
in .m file
-(IBAction)SegmentChange
{
if(segment.selectedSegmentIndex==0)
{
viewA.hidden=NO;
viewB.hidden=YES;
}
else if(segment.selectedSegmentIndex==1)
{
[self.View addSubview:viewB];
viewA.hidden=YES;
viewB.hidden=NO;
}
}

Related

iphone dev: how to control the view transition animation?

I am using such code to present a new view, I don't it's good or not. Currently the default animation is show the view from bottom up, but i want it to animate from right to left(fly in), is it possible to change the default animation type and how? Thanks.
[self presentModalViewController:navController animated:animated];
You can disable the slide-up animation like this:
[self presentModalViewController:navController animated:NO];
Then you can provide your own animation code:
navController.view.frame = CGRectMake(320, 0, navController.view.frame.size.width, navController.view.frame.size.height);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
navController.view.frame = CGRectMake(0, 0, navController.view.frame.size.width, navController.view.frame.size.height);
[UIView commitAnimations];
This example gives you the "Fly-in" from right with a smooth speed-curve.
Another way is using the built in slide-in from right with navigationcontroller:
[self.navigationController pushViewController:navController animated:YES];
In this one, your top-viewcontroller needs to be a UINavigationController and it's rootcontroller needs to be your viewcontroller. Then you can push other viewcontrollers.
I have done this and its working for me try ths:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.View2.superview cache:YES];
[self.View2 removeFromSuperview];
[self performSelector:#selector(replaceViews2) withObject:nil afterDelay:0.6];
[UIView commitAnimations];

Change between UIViewcontroller.view subviews with animation

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

I have 2 UIView: 1st is self.view and movieView( maked in XIB ). I can't see video in this UIView, only audio track

moviePlayerController.initialPlaybackTime = (int)(moviePlayerController.duration / slice ) * source.tag;
[movieView addSubview: moviePlayerController.view];
[[self view] addSubview:movieView];
[moviePlayerController play];
With click custom PLAY button my self.view flips to movieView ... i see my made in XIB movieView, w/o video (
Thank you
Ok. my bad. my second View has been UIImageView and i did change to UIView..
if i insert
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:[self view]
cache:YES];
[UIView commitAnimations];
self.view = moviePlayerController.view;
[moviePlayerController stop];
[moviePlayerController play];
Then appear movie w/o FlipAnimation - switch beetwen mainView and movieView
if i'm insert [[self view] addSubview:movieView] then i'll look only created in XIB playerView ( linked to movieView ), but no see video, only hear audio. what a problem?
Just need use in init section:
[self.view insertSubview:startView atIndex:0];
[self.view insertSubview:videoView atIndex:1];
and in change action write this:
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
and return to startView write again:
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
like
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
[UIView commitAnimations];

Why do I see jerky animation when using this UIView animation block?

I have a a tab bar that is made in the application delegate. By calling an action form a button click from one of the views loaded from the tab bar, I open the help screen but there is a jerking motion after loading.
forgive me for speaking informally..I have been picking my brain for the past few hours trying to figure this out..
-(void)flipToHelp {
HelpViewController *helpVariable = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[self setHelpViewController:helpVariable];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:_window cache:YES];
[_window removeFromSuperview];
[helpVariable release];
self.window.rootViewController = self.HelpViewController;
[UIView commitAnimations];
}
Just to reiterate from the comment thread, you shouldn't be removing the window from its superview (it doesn't technically have a superview, so it's probably causing problems). And setting the window's rootViewController property should swap out the view hierarchies, Apparently the jerkiness comes from changing the window's rootViewController property, so maybe the solution is to avoid using that property. Here's what I think should be enough to accomplish this:
-(void)flipToHelp {
HelpViewController *helpVariable = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_window cache:YES];
[self.tabBarController removeFromSuperview];
[_window addSubview:helpVariable];
[UIView commitAnimations];
}
-(void)flipToHelp {
HelpViewController *helpVariable = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[self setHelpViewController:helpVariable];
[helpVariable release];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.window
cache:YES];
self.window.rootViewController = self.HelpViewController;
[UIView commitAnimations];
}
How about this code? Does it still have a jerky animation?

Problem Flipping a View

I'm having some trouble getting a view to flip. I have the following code in my View Controller:
- (void)loadFlipsideViewController {
ProblemViewFlipController *viewController = [[ProblemViewFlipController alloc] initWithNibName:#"ProblemViewFlip" bundle:nil];
self.problemViewFlipController = viewController;
[viewController release];
}
- (void) flipView {
if (problemViewFlipController == nil) {
[self loadFlipsideViewController];
}
UIView *mainView = self.view;
UIView *flipView = problemViewFlipController.view;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.view cache:YES];
if ([flipView superview])
{
[flipView removeFromSuperview];
[self.view addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[self.view addSubview:flipView];
}
[UIView commitAnimations];
}
The problem is, is that when I call flipView, the view is replaced with a blank view (i.e. nothing in the view I'm flipping to is displayed).
Is there something obvious I'm missing here? (I suspect there is!)
Not positive, but I think you need to use a 'controller' to flip the views. Looks like you're using one of the flipped views as the controller. Just add a root controller to flip your views.
Code like this should work from the root controller:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; // self.view is the root controller's view
[mainViewController viewWillAppear:YES];
[flipViewController viewWillDisappear:YES];
[flipViewController.view removeFromSuperview];
[self.view addSubview:mainViewController.view];
[flipViewController viewDidDisappear:YES];
[mainViewController viewDidAppear:YES];
[UIView commitAnimations];
Based on the variable names, it looks like this is adapted from the default "Utility" application template you get from XCode. If the template project works, and yours doesn't, then you've obviously changed something you shouldn't have :-)
Chances are, one of the outlets in your controller that should be pointing at a view, isn't. Double-check your Nibs, and check the values of the outlets in the debugger. If all else fails, start over again from the template, and see at what point it stops working.