Switching views with animation in iPhone - 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.

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.

how to avoid the refreshing of view1 while switching from view1 to view2 via Segue

I have 2 views and switching from view1 to view2 via a modal segue (full screen, cross dissolve).
When i go to view2 and go back to view1 using the buttons i defined, all the images in view1 are reseted back to their default values.
how can i prevent this?
here if you take 2 view in your XIB after that if you handle this view with button clicked event ,is very easy....
Means first take 2 view from xib after set your control on your view after when you click on butto the hide view1 and show view2 and so on...
like...
if you want to use animation the its work like bellow....
-(IBAction)btnView1_Clicked:(id)sender{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:appDelegate.window cache:YES];
[UIView commitAnimations];
view1.hidden=FALSE;
view2.hidden=TRUE;
}
-(IBAction)btnView2_Clicked:(id)sender{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:appDelegate.window cache:YES];
[UIView commitAnimations];
view1.hidden=TRUE;
view2.hidden=FALSE;
}
here you can hadle your view with this logic...
hope,this help you....
:)

how to make the animation between one view to another view in uitabbarcontroller

I want the animation between the two viewcontrollers in uitabbarcontroller.
I am having the two tabbar. The first one open by default when run the app.I want the animation,while i go to the second tabbar.How can i do this.
Thanks
In the View Will appear of your firstViewController you should have something like
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
}
Then in the secondviewcontroller have the same thing but
UIViewAnimatinTransitionFlipFromleft
However let me know how this goes, because for some reason in the app I was making I had a little trouble on the view first transition then it would work fine after that. If you suffer from the same problem maybe we can work together for a solution.

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.

Custom Animation for Pushing a UIViewController

I want to show a custom animation when pushing a view controller: I would like to achieve something like an "expand" animation, that means the new view expands from a given rectangle, lets say [100,100 220,380] during the animation to full screen.
Any suggestions where to start, respectively any documents, tutorials, links? :)
Alright. I could make the expand animation with the following code:
if ([coming.view superview] == nil)
[self.view addSubview:coming.view];
coming.view.frame = CGRectMake(160,160,0,0);
[UIView beginAnimations:#"frame" context:nil];
[UIView setAnimationDuration:4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[coming viewWillAppear:YES];
[going viewWillAppear:YES];
coming.view.frame = CGRectMake(0, 0, 320, 480);
[going viewDidDisappear:YES];
[coming viewDidAppear:YES];
[UIView commitAnimations];
My View is properly displayed, but unfortunately the navigation bar is not updated. Is there a way to do that manually?
In the sample code, a function is called all 0.03 seconds that updates the transformation of the view.
Unfortunately, when pushing a UIViewController, I am not able to resize the frame of the view ... am I ?
I use the following function (added to UINavigationController) to customize the push animation:
- (void) pushController: (UIViewController*) controller
withTransition: (UIViewAnimationTransition) transition
{
[UIView beginAnimations:nil context:NULL];
[self pushViewController:controller animated:NO];
[UIView setAnimationDuration:.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[UIView commitAnimations];
}
I guess you could adapt this code to do whatever animation you want.
The code which you are looking for:
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:0.80];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:
UIViewAnimationTransitionFlipFromRight
forView:self.navigationController.view cache:NO];
[self.navigationController pushViewController:menu animated:YES];
[UIView commitAnimations];
What you could do is push the next view controller but don't animate it, like so:
[self.navigationController pushViewController:nextController animated:NO];
...and then, in the view controller that is getting pushed in, you could do a custom animation of it's view using CoreAnimation. This might be best done in the viewDidAppear:(BOOL)animated method.
Check out the Core Animation Guide on how to actually do the animation. Look particularly at the implicit animation.
EDIT: updated link
#zoul: That worked great! I just changed "self" to "self.navigationController" and "self.view" to "self.navigationController.view" Don't know if that was necessary, but it worked. And #crafterm, as for popping back, just make your own leftBarButtonItem by adding this code in viewDidLoad or ViewWillAppear:
//add your own left bar button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonTapped)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
Then I just tweaked the push function and made this popWithTransition function that I called in my -backButtonTapped method.
- (void) popWithTransition: (UIViewAnimationTransition) transition
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:transition forView:self.navigationController.view cache:YES];
[UIView commitAnimations];
[self.navigationController popViewControllerAnimated:NO];
}
Note that the popViewController call got shifted down to the end, after the animation. Don't know if that's kosher, but again, it worked.
What you want is the downloads for chapter 2 of iphone developers cookbook. Look at the affineRotate sample specifically, although any of the core animatin samples will help you.
Have a look at ADTransitionController, a drop in replacement for UINavigationController with custom transition animations (its API matches the API of UINavigationController) that we created at Applidium.
You can use different pre-defined animations for push and pop actions such as Swipe, Fade, Cube, Carrousel and so on. In your case, the animation you are requesting is the one called Zoom.