在 iOS 开发中,在同一屏幕上的子 ViewControllers 之间转换是一种常见的场景。以下是实现此功能的一些建议和步骤:
使用 UIViewControllerTransitionCoordinator
可以方便地在两个子 ViewControllers 之间实现动画过渡。以下是一个简单的例子:
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Customize the view accordingly
}
}
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Customize the view accordingly
}
}
class ViewController: UIViewController, UIViewControllerTransitionCoordinator {
let firstViewController = FirstViewController()
let secondViewController = SecondViewController()
override func viewDidLoad() {
super.viewDidLoad()
// Add the first and second view controllers
addChildViewController(firstViewController)
addChildViewController(secondViewController)
// Set the initial view controller
setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
}
在 Storyboard 中,你可以使用 UIViewControllerTransitionCoordinator
实现 ViewController 之间的过渡效果。以下是一个例子:
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Customize the view accordingly
}
}
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Customize the view accordingly
}
}
class ViewController: UIViewController {
let firstViewController = FirstViewController()
let secondViewController = SecondViewController()
override func viewDidLoad() {
super.viewDidLoad()
// Add the first and second view controllers
addChildViewController(firstViewController)
addChildViewController(secondViewController)
// Set the initial view controller
setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)
// Add the first and second view controllers to the container view
let containerView = UIView(frame: view.frame)
addSubview(containerView)
containerView.addSubview(firstViewController.view)
containerView.addSubview(secondViewController.view)
}
}
你还可以手动实现两个 ViewController 之间的过渡效果。例如,你可以创建一个新的 UIView
,将其添加到屏幕顶部,并在其中播放两个 ViewController 之间的过渡动画。在 viewDidLoad
方法中添加以下代码:
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Customize the view accordingly
}
}
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Customize the view accordingly
}
}
class ViewController: UIViewController {
let firstViewController = FirstViewController()
let secondViewController = SecondViewController()
override func viewDidLoad() {
super.viewDidLoad()
// Add the first and second view controllers
addChildViewController(firstViewController)
addChildViewController(secondViewController)
// Set the initial view controller
setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)
// Add the first and second view controllers to the container view
let containerView = UIView(frame: view.frame)
addSubview(containerView)
containerView.addSubview(firstViewController.view)
containerView.addSubview(secondViewController.view)
// Add the custom transition view to the container view
let transitionView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
transitionView.backgroundColor = UIColor.red
containerView.addSubview(transitionView)
// Present the second view controller
secondViewController.view.frame = view.frame
secondViewController.view.alpha = 0
transitionView.frame = CGRect(
领取专属 10元无门槛券
手把手带您无忧上云