美文网首页iOS18适配指南
iOS18适配指南之UIViewController

iOS18适配指南之UIViewController

作者: YungFan | 来源:发表于2024-08-25 10:00 被阅读0次

    介绍

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

    使用

    zoom效果

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

    coverVertical效果

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

    flipHorizontal效果

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

    crossDissolve效果

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

    partialCurl效果

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

    相关文章

      网友评论

        本文标题:iOS18适配指南之UIViewController

        本文链接:https://www.haomeiwen.com/subject/wicnkjtx.html