UIView animation transition between views - iphone

I need to make an animation that will come in from the right, displaying the new view.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelay:.5f];
[UIView setAnimationDuration:0.5f];
// display view controller
[UIView commitAnimations];
What should I add here??

IF you have navigationController then use
NextViewcontroller *nextView = [NextViewcontroller alloc]initWithNibName#"NextViewcontroller"
bundle:nil];
[self.navigationController pushViewcontroller:nextView animated:YES];
[nextView release];

How about adding the view as a subView to your current view, then set its place to be outside the current visible area. After which you set your animation to change your subViews position to overlap the current view.

Related

UINavigationController custom animation [duplicate]

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.

Is it possible to set bounds for the view before it is shown

-(void)CallingView2{
SettingsViewController *aSettingsView = [[SettingsViewController alloc] initWithNibName:#"Settings" bundle:nil];
[self setSettingsViewController:aSettingsView];
[aSettingsView release];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
//setting the animation
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[self.window addSubview:[settingsViewController view]];
**[[settingsViewController view] setBounds:CGRectMake(0, -30, 320, 480)];**
[UIView commitAnimations];}
I have put the code between the stars in the code where I commit my animation and it works, it moves the view as it should but now the problem is that when i rotate to the view i can see when the view is moving down.
Is it possible to set bounds for the view before it is shown so the user cant see when it is moving 30px down every time he go to settings
Your problem is that you're setting the view bounds within an animation block. The view origin will animate from (0,0) to (0,-30) during the time it takes to perform the flip transition.
Setting your view geometry in the view controller's viewDidLoad would be a better approach.

Flip animation when controller pushed on iPhone

I had a look around and didn't find what I was exactly looking for.
Is there a way to get a flip animation when pushing a view controller?
I read that you can change the animation by using a modal view controller but AFAIK the animation for a modal view is from bottom to top and that's not what i am looking for. Is there a way to get a flip animation somehow?
something like this should work
[UIView beginAnimations:#"animation" context:nil];
[self.navigationController pushViewController: yourviewcontroller animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView commitAnimations];
don't forget to set animated to NO when calling pushViewController
This also works.. for iOS 4.0 and greater
[UIView transitionWithView:self.navigationController.view duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^(void) {
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
[self.navigationController pushViewController:viewController animated:YES];
[UIView setAnimationsEnabled:oldState];
}
completion:nil];
- (void)viewWillDisappear:(BOOL)animated {
[UIView beginAnimations:#"animation2" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration: 0.7];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView commitAnimations]; }
in the new viewcontroller will make it flip back the same way (instead of sliding left) when the back button in the toolbar is pushed -- make sure animation is enabled here, e.g., if you make a custom button to pop the stack, use:
- (void) backToPrevious: (id) sender
{
//[self.navigationController popViewControllerAnimated:YES];
[self dismissModalViewControllerAnimated:YES];
}
For modally presented view controllers, you can change the animation with the modalTransitionStyle property. AFAIK, there is no way to change a navigation controller's push animation (except rebuilding UINavigationController from scratch).

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.

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.