iosswiftanimationcatransition

Push Up View Controller from Bottom using Swift


I would like to push a view controller using Swift and animate it so it appears from the bottom and moves up. I have the following code to push my view controller:

let helloTableViewController = self.storyboard!.instantiateViewControllerWithIdentifier("helloTableViewController") as! HelloTableViewController
self.navigationController!.pushViewController(helloTableViewController, animated: true)

I have found the following from another question but cannot seem to get it to work in Swift:

CATransition *animation = [CATransition animation]; 
[animation setDuration:2]; 
[animation setType:kCATransitionPush]; 
[animation setSubtype:kCATransitionFromTop]; 
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
SecondView *sObj=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
[self.navigationController pushViewController:sObj animated:YES];
[[sObj.view layer] addAnimation:animation forKey:@"SwitchToView1"];

Solution

  • Here is the code for pushing

    let transition:CATransition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
    transition.type = CATransitionType.push
    transition.subtype = CATransitionSubtype.fromBottom
    self.hostNavController?.view.layer.add(transition, forKey: kCATransition)
    self.hostNavController?.popViewController(animated: true)
    

    and type of transition you can use are

    kCATransitionFromLeft
    kCATransitionFromBottom
    kCATransitionFromRight
    kCATransitionFromTop