当前位置: 首页 > article >正文

使用QMUI实现用户协议对话框

使用QMUI实现用户协议对话框

懒加载用于初始化 TermServiceDialogController 对象。

懒加载 lazy var 的作用

lazy var dialogController: TermServiceDialogController = {
    let r = TermServiceDialogController()
    r.primaryButton.addTarget(self, action: #selector(primaryClick), for: .touchUpInside)
    return r
}()

1. 延迟初始化 (lazy)

​ • dialogController 是懒加载的,也就是说 只有第一次访问时才会创建 TermServiceDialogController 实例

​ • 这样可以节省内存和性能开销,避免未必要的对象初始化。

2. 自定义初始化过程

​ • 在 lazy 闭包中配置 dialogController 的属性:

r.primaryButton.addTarget(self, action: #selector(primaryClick), for: .touchUpInside)

这里为 primaryButton 按钮绑定了点击事件 primaryClick。

3. 返回配置完成的对象

​ • 返回已经配置好的 TermServiceDialogController 实例,供外部直接使用。

执行顺序

​ 1. 当你第一次访问 dialogController 时:

​ • 执行闭包内容,创建 TermServiceDialogController 对象。

​ • 为 primaryButton 绑定点击事件。

​ • 返回该对象。

​ 2. 之后的访问直接返回已经创建的实例,不再重复执行闭包。

调用示例

dialogController.show()  // 弹出对话框

点击 primaryButton 后触发 primaryClick 事件:

@objc func primaryClick() {
    print("Primary button clicked!")
}

为什么要用 lazy?

​ • 避免不必要的初始化,提升性能。

​ • 当 dialogController 未使用时,不会占用内存

​ • 可以在懒加载闭包中完成复杂的初始化和配置。

这种方式常用于配置复杂 UI 控件或管理弹窗对象。

关于 QMUIModalPresentationViewController 的作用

QMUIModalPresentationViewController 是 QMUI 框架 提供的一种模态弹框组件,它对系统原生的 UIViewController 的模态展示 (present) 进行了封装和增强,提供了更多的动画样式、布局控制以及弹框管理功能。

modalController.contentViewController = self

modalController 来管理整个弹窗布局。

具体视图层级:

QMUIModalPresentationViewController
└── TermServiceDialogController.view
    └── contentContainer (TGLinearLayout)
        ├── titleView (UILabel)
        ├── textView (UITextView)
        ├── primaryButton (QMUIButton)
        └── disagreeButton (QMUIButton)

设置弹窗大小

        //设置弹窗的大小
        view.tg_width.equal(.fill)
        view.tg_height.equal(.wrap)

内容容器

        //内容容器
        contentContainer = TGLinearLayout(.vert)
        contentContainer.tg_width.equal(.fill)
        contentContainer.tg_height.equal(.wrap)
        contentContainer.tg_space = 25
        contentContainer.backgroundColor = .white
        contentContainer.tg_gravity = TGGravity.horz.center
        //设置内容容器的边距
        contentContainer.tg_padding = UIEdgeInsets(top: PADDING_LARGE2, left: PADDING_LARGE2, bottom: PADDING_LARGE2, right: PADDING_LARGE2)
        contentContainer.tg_gravity = TGGravity.horz.center
        view.addSubview(contentContainer)

show函数

  func show() {
      modalController = QMUIModalPresentationViewController()

      //渐变效果
      modalController.animationStyle = .fade

      //点击外部不隐藏
      modalController.isModal = true

      //设置要显示的内容控件
      modalController.contentViewController = self

      modalController.showWith(animated: true) ;

  }

流程顺序

外界调用 dialog.show()

创建 QMUIModalPresentationViewController

配置动画、模态属性、内容控制器

modalController.showWith(animated: true)

将 contentViewController.view(即 dialog.view)添加到弹窗内容

执行淡入动画并展示弹窗


http://www.kler.cn/a/529736.html

相关文章:

  • 电脑要使用cuda需要进行什么配置
  • 数据分析系列--②RapidMiner导入数据和存储过程
  • 4 [危机13小时追踪一场GitHub投毒事件]
  • 全程Kali linux---CTFshow misc入门(25-37)
  • JVM运行时数据区域-附面试题
  • 浅色可视化大屏虽然经常被诟病,也有自己的用武之地呀
  • 第 1 天:UE5 C++ 开发环境搭建,全流程指南
  • [Linux]从零开始的STM32MP157 U-Boot移植
  • Python(Pandas)数据分析学习
  • lstm代码解析1.2
  • 《手札·开源篇》从开源到商业化:中小企业的低成本数字化转型路径——一位甲方信息化负责人与开源开发者的八年双重视角
  • 【Qt】Qt老版本解决中文乱码
  • ESP32-c3实现获取土壤湿度(ADC模拟量)
  • R语言统计分析——数据类型
  • 【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】2.9 广播陷阱:形状不匹配的深层隐患
  • 【TypeScript】基础:数据类型
  • GIS教程:全国数码商城系统
  • 【C语言练习题】圣经数
  • 自定义数据集 ,使用朴素贝叶斯对其进行分类
  • 蓝桥杯例题六
  • 如何在Windows、Linux和macOS上安装Rust并完成Hello World
  • OpenGL学习笔记(五):Textures 纹理
  • 深入解析 vmstat 命令的工作原理
  • 海思ISP开发说明
  • 2025年Android开发趋势全景解读
  • 基于java SSM的房屋租赁系统设计和实现