在swift中从一个视图控制器转到另一个视图控制器
我需要从一个视图控制器切换到另一个视图控制器,而无需执行任何操作(如单击按钮),这样我就可以在第一个视图控制器中放置一些动画,然后再切换到第二个视图控制器。尝试这些简单导航的步骤,只需更改动画真实的标志即可。 如果您想学习控制器转换动画,可以阅读这些教程。 步骤1-在ViewController上创建按钮,并在ViewController上创建按钮的iAction在swift中从一个视图控制器转到另一个视图控制器,swift,animation,viewcontroller,Swift,Animation,Viewcontroller,我需要从一个视图控制器切换到另一个视图控制器,而无需执行任何操作(如单击按钮),这样我就可以在第一个视图控制器中放置一些动画,然后再切换到第二个视图控制器。尝试这些简单导航的步骤,只需更改动画真实的标志即可。 如果您想学习控制器转换动画,可以阅读这些教程。 步骤1-在ViewController上创建按钮,并在ViewController上创建按钮的iAction @IBAction func pressButton(sender: UIButton) { navigateT
@IBAction func pressButton(sender: UIButton) {
navigateToSecondViewController()
}
func navigateToSecondViewController() {
let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.present(secondVC, animated: true, completion: nil)
}
func navigateToSecondViewController() {
let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(secondVC, animated: true)
}
步骤2-创建导航到secondViewController的函数
@IBAction func pressButton(sender: UIButton) {
navigateToSecondViewController()
}
func navigateToSecondViewController() {
let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.present(secondVC, animated: true, completion: nil)
}
func navigateToSecondViewController() {
let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(secondVC, animated: true)
}
步骤3-如果您使用的是NavigationController,则可以按下控制器
@IBAction func pressButton(sender: UIButton) {
navigateToSecondViewController()
}
func navigateToSecondViewController() {
let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.present(secondVC, animated: true, completion: nil)
}
func navigateToSecondViewController() {
let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(secondVC, animated: true)
}
使用
UIView.animate()
可以在第一个视图控制器的viewdispect()
方法中执行任何动画,并在完成块中加载第二个控制器,如下所示:
UIView.animate(withDuration: 1.0, delay: 0, options: .curveEaseIn, animations:
{
//add animation code here
}) { (isAnimationComplete) in
//load new controller here
let controller = SecondViewController()
navigationController.pushViewController(controller, animated: true)
}
1:转到情节提要,在第一个和第二个视图控制器之间创建一个手动分段(控件单击并从第一个视图控制器上方的黄色圆圈拖动到第二个视图控制器主体) 2:点击背景中的segue并转到属性检查器(右上面板中的第4个按钮),在“标识符”文本字段中,输入“SegueName”(或任何您想要的内容) 3:转到FirstViewController的.swift文件并添加performSegue函数
class FirstViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.performSegue(withIdentifier: "SegueName", sender: self)
}
}
将performsgue放在viewDidLoad()中会导致应用程序在屏幕加载后立即退出。如果您需要帮助编码动画(或设置为在FirstViewController上运行的任何代码)后发生的序列,我需要查看两个视图控制器的文件。他希望不需要任何操作,而您已经添加了一个带有操作的按钮。他提到,在这里可以执行任何像按钮一样的操作,我创建了导航到第二个控制器的功能,这样您就可以在任何地方调用,而只需调用其他功能。这个答案很好,我给出了过渡动画的教程链接,一个简单的谷歌搜索可以引导你到达目的地。