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

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
iOS developer
  • 131
  • 1
  • 1
  • 9

1 Answers1

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)
  }
Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
Dinesh
  • 1,137
  • 9
  • 14
  • @iOSdeveloper Thanks :-) – Dinesh Apr 14 '16 at 11:09
  • I would have upvoted but i am not eligible to up-vote yet.But i will definitely do that once i earn that reputation. – iOS developer Apr 14 '16 at 11:36
  • @iOSdeveloper Thanks :-) ... Thats so nice of you :-) – Dinesh Apr 14 '16 at 11:59
  • I have converted the code to swift but it is not working in pop – Nex Mishra May 13 '16 at 23:02
  • @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 ? – Surjeet Singh Nov 03 '16 at 06:19
  • 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? – Sumit Jain Nov 15 '17 at 07:49
  • If it doesn't work for you or looks weird, check if the `animated` property is set to `false`. – Jan Erik Schlorf Jun 06 '18 at 09:03