使用Swift从底部向上推视图控制器

时间:2015-08-12 16:22:10

标签: ios swift animation catransition

我想使用Swift推动一个视图控制器并为其设置动画,使其从底部显示并向上移动。我有以下代码来推动我的视图控制器:

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

我从another question找到了以下内容,但似乎无法在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"];

6 个答案:

答案 0 :(得分:31)

Swift3:

for push:

func closeView() {
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionReveal
    transition.subtype = kCATransitionFromBottom
    navigationController?.view.layer.add(transition, forKey: nil)
    _ = navigationController?.popViewController(animated: false)
}

和vc for pop:

reportData = futureData.then(…)

答案 1 :(得分:19)

以下是推送

的代码
  let transition:CATransition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromBottom
    self.navigationController!.view.layer.add(transition, forKey: kCATransition)
    self.navigationController?.pushViewController(dstVC, animated: false)

和您可以使用的转换类型

 kCATransitionFromLeft
 kCATransitionFromBottom
 kCATransitionFromRight
 kCATransitionFromTop

答案 2 :(得分:3)

我有一个类似的情况,我需要在视图控制器上显示带有一些UIBarButtonItems的导航项,但是以模态显示时却没有显示。它仅在从navigationController推入时显示,但是我确实需要从下至上动画。 我从上面尝试了CATransition示例,但它带来了另一个错误,该错误在动画过程中有黑色阴影/半透明背景。 最后,我想出了一个非常简单的解决方案:

ls_ctu_params

要关闭视图控制器并完全摆脱导航控制器,只需调用:

let someVC = storyboard?.instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
let navController = UINavigationController(rootViewController: someVC)
navigationController?.present(navController, animated: true, completion: nil)

这将关闭视图控制器,并从内存中释放我们的navigationController。

最后这是一个简单的解决方案,但我花了一些时间才提出解决方案。也许我会节省别人的时间:)

答案 3 :(得分:2)

已使用Swift 5和Xcode 11.4更新

以当前动画样式推送控制器

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController

    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    transition.type = CATransitionType.moveIn
    transition.subtype = CATransitionSubtype.fromTop
    self.navigationController?.view.layer.add(transition, forKey: nil)
    self.navigationController?.pushViewController(viewController, animated: false)

关闭样式的PopToView控制器

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

答案 4 :(得分:0)

基于Nex Mishra's answer的Swift 4.2+解决方案:

let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
transition.type = .push
transition.subtype = .fromBottom
navigationController?.view.layer.add(transition, forKey: kCATransition) 
navigationController?.pushViewController([destination VC], animated: false)

答案 5 :(得分:0)

Swift 5.0及更高版本

  let transition:CATransition = CATransition()
  transition.duration = 0.5
  transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
  transition.type = CATransitionType.push
  transition.subtype = CATransitionSubtype.fromTop
  self.navigationController!.view.layer.add(transition, forKey: kCATransition)