Push and Pop animations in a navigation based app - ios

I have a navigation based application and I want to change the animation of the push and pop animations. How would I do that?
When i push any viewController it represent as pop and When i pop any viewController it represent as push.

CATransition *transition = [CATransition animation];
transition.duration = 0.3f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype=kCATransitionFromRight;
kCATransitionFromLeft or kCATransitionFromRight
[self.navigationController.view.layer addAnimation:transition forKey:nil];
add this before you push the view controller for custom animation

-(void)pushViewController:(UIViewController *)viewController{
float width = self.frame.size.width;
float height = self.frame.size.height;
[viewController.view setFrame:CGRectMake(width, 0.0, width, height)];
[self addSubview:viewController.view];
[UIView animateWithDuration:0.33f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
[viewController.view setFrame:self.frame];
[self setFrame:CGRectMake(0.0, 0.0, width, height)];
}
completion:^(BOOL finished){
}];
}

Related

How to Push a ViewController from Left Over another

I want to make a Navigation Drawer Like This:
I saw many third party libararies. But I want to do it from my own.
Here is what I am trying :
__block UIViewController *sourceViewController = self;
NavigationDrawerVC *controller = [[NavigationDrawerVC alloc] initWithNibName:#"NavigationDrawerVC" bundle:nil];
__block UIViewController *destinationController = (UIViewController*)controller;
CATransition* transition = [CATransition animation];
transition.duration = 5;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
But I am getiing o/p like this:
When I click on Button My Screen Changes Like this:
My Previous VIewController also moving.
I want to make it Fix. And also I am not able to stop it at certain point.
You might want to look into using a child controller for this.
Here's a very simplified code which demonstrates this concept. It adds a child view controller and animates it to the middle of the screen. This should at least give you a starting point:
UIViewController *drawerVC = [[UIViewController alloc] init];
drawerVC.view.backgroundColor = [UIColor redColor];
[self addChildViewController:drawerVC];
drawerVC.view.frame = CGRectMake(-self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:drawerVC.view];
[drawerVC didMoveToParentViewController:self];
[UIView animateWithDuration:0.3 animations:^{
drawerVC.view.frame = CGRectOffset(drawerVC.view.frame, drawerVC.view.frame.size.width * 0.5, 0);
}];

PresentViewController with custom animation

I need to present view controller with UIViewAnimationOptionCurveEaseIn animation where source controller disappear from 1 alpha to 0 and destination controller appear from 0 alpha to 1
I'd like to use
presentViewController: animated: completion:
I've tried:
In source controller
UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:#"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];
PopViewController *pop = [st instantiateViewControllerWithIdentifier:name];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:pop];
nav.navigationBarHidden = YES;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.view.alpha = 0.f;
} completion:^(BOOL finished) {
[self presentViewController:nav animated:NO completion:nil];
}];
In destination controller
- (void)viewDidLoad{
[super viewDidLoad];
self.view.alpha = 0.f;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.view.alpha = 1.f;
} completion:^(BOOL finished) {
}];
but controllers disappear and appear not at the same time
Pavel, for example
-(void)goToViewControllerAction
{
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
CATransition *transition = [CATransition animation];
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
transition.duration = 1.5;
[transition setType:kCATransitionFade];
[self.navigationController.view.layer addAnimation:transition forKey:#"someAnimation"];
[self performSegueWithIdentifier:#"backPopsegue" sender:nil];// Here your push or performSegue transition
[CATransaction commit];
}
Don't remember to use QuartzCore)
You can customize your transition like you want, there are a lot of different settings in it....

Add Slide In from Left to Right animation(transition) in addSubView

I Try to imitate the NavigationViewController, very similar to default MailApp in iPhone
When clicking on a Mail summary, it should slide-in the mail details, and when BACK button is clicked, Mail Summary view is to be slided-in back again.
This is what I have to animate the transition from Summary To Detail (removeFromSuperView)
CGRect temp = self.view.frame;
temp.origin.x = 300;
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
self.view.frame = temp;
}completion:^(BOOL finished){
[self.view removeFromSuperview];
}];
This is what I have to animate the transition from Detail To Summary (addSubview)
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionFromRight;
transition.subtype = kCATransitionFade;
[parentView.layer addAnimation:transition forKey:nil];
[parentView addSubview:myVC.view];
Now, that My First part of code is working well! Where-as the second part, I am just able to achieve the Fade-in animation. How can I bring in the slide transition?
I just need to choose kCATransitionPush type for my CATransition
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[parentView.layer addAnimation:transition forKey:nil];
[parentView addSubview:myVC.view];
Update for Swift:
let transition = CATransition()
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
parentView.layer.add(transition, forKey: nil)
parentView.addSubview(myVC.view)
Or you could check out : HorizontalSlide Segue (just google it I cant get the link, on mobile) its a great sliding transition that is simply a segue that you add from view A to B....
On a side note, if this is in a nav controller, a push or a pop should have a slide animation by default...

Present ViewController with custom animation

I am presenting a view controller using the foll. code:
VC_B * b = [[VC_B alloc] init...];
[self presentViewController:b animated:YES completion:nil];
I am trying to animate appearance of the view controller: the incoming VC should slide down from top covering VC that is currently displayed. following this solution makes it work with one problem: current VC disappears before the incoming VC appears on the screen. Is there a way to resolve this? perhaps there is an alternative solution to achieve this effect.
Try this code :
CreateNewViewController *newViewController = [[CreateNewViewController alloc]initWithNibName:#"CreateNewViewController" bundle:nil];
CATransition *transition = [CATransition animation];
transition.duration = 0.05;
transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type =kCATransitionMoveIn;
transition.subtype =kCATransitionFromTop;
transition.delegate = self;
[self presentModalViewController:newViewController animated:NO];
[self.view insertSubview:newViewController.view atIndex:0];
[self.view.layer addAnimation:transition forKey:nil];
Hope this will help you .
Try the below code. You just need to call below method from the point where you want to display NewViewController. What you need to do is that you just need to change the OriginY of NewViewcOntroller's View Frame.
-(void)displayNewVC
{
YourNewViewController *newVC = [[YourNewViewController alloc] init];
CGFloat originY = -450;//set originY as you want to display newViewController from the Top to bottom with animation
//initially set originY out of the Frame of CurrentViewcontroller
newVC.view.frame = CGRectMake(orginX, originY, newVC.view.frame.size.width, newVC.view.frame.size.height);
/*Display View With Animation*/
originY = 300;
[UIView animateWithDuration:.3 animations:^{
//set new originY as you want.
newVC.view.frame = CGRectMake(orginX, originY, newVC.view.frame.size.width, newVC.view.frame.size.height);
} completion:NULL];
[currentViewController.view addSubview:newVC.view];
}

Showing pushviewcontroller animation look like presentModalViewController

Is it possible to show pushViewController animation look like presentModalViewController but that has background functionality of pushViewController?
If you want to a fade animation, this approach works.
CATransition* transition = [CATransition animation];
transition.duration = 0.3;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromTop;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:gridController animated:NO];
Try this :
UIViewController *yourViewController = [[UIViewController alloc]init];
[UIView beginAnimations: #"Showinfo"context: nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController: yourViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[UIView commitAnimations];
For display PushViewConttroler animation as Present below code is working,
For Push
ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
CATransition* transition = [CATransition animation];
transition.duration = 0.4f;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromTop;
[self.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[self.navigationController pushViewController:VC animated:NO];
And for POP
CATransition* transition = [CATransition animation];
transition.duration = 0.4f;
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromBottom;
[self.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[self.navigationController popViewControllerAnimated:NO];
There is 1 issue though, that its display light black background when its going up/down,
Working on that, As soon as its done post new code here.
For All Trying in Xamarin (C#) equivalent Code
From Answer by #hardik Thakkar above, Has the issue of Black backgound during animation, rest all good.
For POP
CATransition transition = CATransition.CreateAnimation();
transition.Duration = 0.4;
transition.Type = CAAnimation.TransitionReveal;
transition.Subtype = CAAnimation.TransitionFromTop;
this.NavigationController.View.Layer.AddAnimation(transition, nameof(CATransition));
For Push
CATransition transition = CATransition.CreateAnimation();
transition.Duration = 0.4;
transition.Type = CAAnimation.TransitionReveal;
transition.Subtype = CAAnimation.TransitionFromBottom;
this.NavigationController.View.Layer.AddAnimation(transition, nameof(CATransition));
You should consider doing something like:
ViewController *myVC = [[ViewController alloc] initWithFrame:OFF_SCREEN];
[navigationController pushViewController:myVC animated:NO];
where OFF_SCREEN is some CGRect below the screen, like (0, 481, 320, 480). Then use an animation block to adjust the viewcontroller's view's frame.origin to be (0, 0). You would probably want to do the animate block in viewDidLoad or viewDidAppear.

Resources