前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS18适配指南之UIViewController

iOS18适配指南之UIViewController

原创
作者头像
YungFan
发布2024-09-09 11:32:14
2030
发布2024-09-09 11:32:14
举报
文章被收录于专栏:学海无涯

介绍

增加了类型为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)
    }
}
zoom
zoom

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

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

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

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 使用
    • zoom效果
      • coverVertical效果
        • flipHorizontal效果
          • crossDissolve效果
            • partialCurl效果
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档