介绍
增加了类型为UIViewController.Transition
的preferredTransition
属性,可以实现特殊的转场效果,共有 5 种效果,分别为zoom
、coverVertical
、flipHorizontal
、crossDissolve
与partialCurl
。
使用
zoom效果
代码语言:swift复制import UIKit
class ViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
button.setImage(UIImage(systemName: "cursorarrow.click.2"), for: .normal)
button.center = view.center
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
}
// MARK: 弹出按钮点击事件
@objc func buttonClicked(_ sender: Any) {
let nextViewController = NextViewController()
// iOS18新增,zoom效果
nextViewController.preferredTransition = .zoom { context in
guard context.zoomedViewController is NextViewController else {
fatalError("Unable to access the current view controller.")
}
// 返回触发的UIView
return self.button
}
present(nextViewController, animated: true)
}
}
class NextViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dismiss(animated: true)
}
}
coverVertical效果
代码语言:swift复制import UIKit
class ViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
button.setImage(UIImage(systemName: "cursorarrow.click.2"), for: .normal)
button.center = view.center
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
}
// MARK: 弹出按钮点击事件
@objc func buttonClicked(_ sender: Any) {
let nextViewController = NextViewController()
// iOS18新增,coverVertical效果
nextViewController.preferredTransition = .coverVertical
present(nextViewController, animated: true)
}
}
class NextViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dismiss(animated: true)
}
}
flipHorizontal效果
代码语言:swift复制import UIKit
class ViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
button.setImage(UIImage(systemName: "cursorarrow.click.2"), for: .normal)
button.center = view.center
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
}
// MARK: 弹出按钮点击事件
@objc func buttonClicked(_ sender: Any) {
let nextViewController = NextViewController()
// iOS18新增,flipHorizontal效果
nextViewController.preferredTransition = .flipHorizontal
present(nextViewController, animated: true)
}
}
class NextViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dismiss(animated: true)
}
}
crossDissolve效果
代码语言:swift复制import UIKit
class ViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
button.setImage(UIImage(systemName: "cursorarrow.click.2"), for: .normal)
button.center = view.center
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
}
// MARK: 弹出按钮点击事件
@objc func buttonClicked(_ sender: Any) {
let nextViewController = NextViewController()
// iOS18新增,crossDissolve效果
nextViewController.preferredTransition = .crossDissolve
present(nextViewController, animated: true)
}
}
class NextViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dismiss(animated: true)
}
}
partialCurl效果
代码语言:swift复制import UIKit
class ViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
button.setImage(UIImage(systemName: "cursorarrow.click.2"), for: .normal)
button.center = view.center
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
}
// MARK: 弹出按钮点击事件
@objc func buttonClicked(_ sender: Any) {
let nextViewController = NextViewController()
// iOS18新增,partialCurl效果
nextViewController.modalPresentationStyle = .fullScreen
nextViewController.preferredTransition = .partialCurl
present(nextViewController, animated: true)
}
}
class NextViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dismiss(animated: true)
}
}