iOS18适配指南之UIViewController

2024-09-09 11:32:14 浏览数 (3)

介绍

增加了类型为UIViewController.TransitionpreferredTransition属性,可以实现特殊的转场效果,共有 5 种效果,分别为zoomcoverVerticalflipHorizontalcrossDissolvepartialCurl

使用

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

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

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

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

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

1 人点赞