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

Push Up View Controller from Bottom using Swift

我想使用 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"];

这里是推送的代码

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)

您可以使用的过渡类型是

kCATransitionFromLeft
kCATransitionFromBottom
kCATransitionFromRight
kCATransitionFromTop

斯威夫特3:

用于推送:

    // push view controller but animate modally
    let storyBoard: UIStoryboard = UIStoryboard(name: "myStoryBoard", bundle: nil)
    let vc = storyBoard.instantiateViewController(withIdentifier: "myViewControllerIdentifier") as! MyViewController

    let navigationController = self.navigationController


    vc.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: vc, action: #selector(vc.closeView))
    vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: vc, action: nil)

    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionMoveIn
    transition.subtype = kCATransitionFromTop
    navigationController?.view.layer.add(transition, forKey: nil)
    navigationController?.pushViewController(vc, animated: false)

并在 vc 中流行:

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)
}

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)

我有过类似的情况,我需要在视图控制器上显示带有一些 UIBarButtonItems 的导航项,但是当以模态方式呈现时,它没有显示。它仅在从 navigationController 推送时显示,但我真的需要从下到上的动画。 我尝试了上面的 CATransition 示例,但它引发了另一个错误,即在动画期间有一个黑色阴影/半透明背景。 最后我想到了一个非常简单的解决方案:

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

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

self.dismiss(animated: true, completion: nil)

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

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

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)

更新为 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)