push uiviewcontroller to another viewcontroller with animation from Top - bottom
Asked Answered
D

1

6

In my project there is feature when user swipe on top bar one screen will appear with top to bottom animation

There are two view controller oneviewcontroller.m

- (void)swipe
{
    listViewController *list_obj=[[listViewController alloc] initWithNibName:@"listViewController" bundle:NULL];
    UIViewAnimationTransition trans = UIViewAnimationTransitionCurlUp;
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationTransition: trans forView: [self.view window] cache: YES];
    [self.navigationController pushViewController:list_obj animated:YES];
    [UIView commitAnimations];
}

But this does not give the animation from top-bottom

I want to implement the navigation from Push := top - bottom Pop : = bottom - top

please help me Thank you

Delilahdelimit answered 14/4, 2016 at 10:35 Comment(0)
U
36

You can push a view controller top to bottom like follows:

Obj-C:

- (void) pushVC:(UIViewController )dstVC {
    CATransition *transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromTop;
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
    [self.navigationController pushViewController:dstVC animated:NO];
}

Use the below code to pop view controller from Bottom to Top:

- (void) popVC {
    CATransition *transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    transition.subtype = kCATransitionFromBottom;
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
    [self.navigationController popViewControllerAnimated:NO];
}

Swift 3:

  func open() {
    let settingsVC = SettingsVC()
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromTop
    navigationController?.view.layer.add(transition, forKey: kCATransition)
    navigationController?.pushViewController(settingsVC, animated: false)
  }

  func close() {
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionFade
    transition.subtype = kCATransitionFromBottom
    navigationController?.view.layer.add(transition, forKey:kCATransition)
    let _ = navigationController?.popViewController(animated: false)
  }
Umbilicate answered 14/4, 2016 at 10:43 Comment(7)
@iOSdeveloper Thanks :-)Umbilicate
I would have upvoted but i am not eligible to up-vote yet.But i will definitely do that once i earn that reputation.Delilahdelimit
@iOSdeveloper Thanks :-) ... Thats so nice of you :-)Umbilicate
I have converted the code to swift but it is not working in popJoeyjoffre
@Microprocessor8085 : I am getting a issue with this. When i used this code, animation works fine as required, but since i am pushing view controller from a viewController which is presented on another, Presented Controller being displayed while animation is in progress. Do anyone have any idea, to avoid this with same view hierarchy ?Verism
The above method slides the current VC up and simultaneously slides the new VC from bottom. I want push animation same as presentViewController()'s animation. Can anyone help?Sob
If it doesn't work for you or looks weird, check if the animated property is set to false.Bornie

© 2022 - 2024 — McMap. All rights reserved.