Sheeeeeeeeet 高级 Action Sheet 开发指南

Sheeeeeeeeet 高级 Action Sheet 开发指南

前言

Sheeeeeeeeet 是一个强大的 iOS 动作表单库,它提供了高度可定制化的界面组件。本文将深入探讨如何创建高级 Action Sheet,包括全局样式设置、复杂表单构建以及自定义子类开发等高级技巧。

全局样式设置

Sheeeeeeeeet 允许开发者通过 appearance() 方法为不同类型的单元格设置全局样式。这种方式类似于 iOS 中的 UIAppearance 机制,可以统一应用样式到所有同类组件。

// 设置普通项目样式
let item = ActionSheetItemCell.appearance()
item.height = 50
item.titleColor = .darkGray
item.titleFont = .systemFont(ofSize: 15)

// 设置标题样式
let title = ActionSheetTitleCell.appearance()
title.height = 60
title.titleColor = .black
title.titleFont = .systemFont(ofSize: 16)

// 设置选择项目样式
let selectItem = ActionSheetSelectItemCell.appearance()
selectItem.selectedIcon = UIImage(named: "ic_checkmark")

样式设置支持多种单元格类型,包括:

  • 基础项目 (ActionSheetItemCell)
  • 标题 (ActionSheetTitleCell)
  • 分段标题 (ActionSheetSectionTitleCell)
  • 单选/多选项目 (ActionSheetSelectItemCell)
  • 按钮 (ActionSheetButtonCell)
  • 确定/取消按钮 (ActionSheetOkButtonCell/ActionSheetCancelButtonCell)

构建复杂动作表单

下面是一个自行车服务预约表单的完整示例,展示了如何组合多种项目类型:

// 定义枚举类型表示业务数据
enum Time { case morning, afternoon }
enum Service { case cleaning, oiling, waxing }

// 创建表单项目
let title = ActionSheetTitle(title: "Bike Service")
let section1 = ActionSheetSectionTitle(title: "Time", subtitle: "Pick one")
let item1_1 = ActionSheetSingleSelectItem(title: "Morning", subtitle: "08:00 - 12:00", 
                                        isSelected: true, group: "time", 
                                        value: Time.morning, tapBehavior: .none)
let ok = ActionSheetOkButton(title: "Book Service")
let cancel = ActionSheetCancelButton(title: "Cancel")

// 组合项目并创建表单
let items = [title, section1, item1_1, ok, cancel]
let sheet = ActionSheet(items: items) { sheet, item in
    guard item is OkButton else { return }
    // 处理表单提交
}

这个表单包含:

  1. 主标题
  2. 时间选择分段
  3. 单选时间选项
  4. 服务选择分段
  5. 多选服务选项
  6. 确定和取消按钮

自定义 Action Sheet 子类

为了复用复杂表单,我们可以创建自定义子类:

class BikeServiceActionSheet: ActionSheet {
    
    init(action: @escaping SelectAction) {
        let items = BikeServiceActionSheet.createItems()
        super.init(items: items, action: action)
    }
    
    var selectedTime: Time? {
        let items = self.items.compactMap { $0 as? ActionSheetSingleSelectItem }
        return items.first { $0.isSelected }?.value as? Time
    }
    
    private static func createItems() -> [ActionSheetItem] {
        // 创建所有项目...
        return [title, section1, item1_1, item1_2, ok, cancel]
    }
}

使用自定义子类的优势:

  • 封装复杂的项目创建逻辑
  • 提供类型安全的属性访问
  • 便于统一修改和维护
  • 简化使用接口

自定义项目子类

进一步,我们可以为特定类型的项目创建子类:

class TimeItem: ActionSheetSingleSelectItem {
    var time: Time? { return value as? Time }
}

class ServiceItem: ActionSheetMultiSelectItem {
    var service: Service? { return value as? Service }
}

然后在自定义 Action Sheet 中使用这些子类:

let item1_1 = TimeItem(title: "Morning", subtitle: "08:00 - 12:00", 
                      isSelected: true, group: "time", 
                      value: Time.morning, tapBehavior: .none)

自定义项目样式

要为自定义项目设置独特样式,需要重写 cell 方法:

class TimeItem: ActionSheetSingleSelectItem {
    override func cell(for tableView: UITableView) -> ActionSheetItemCell {
        return TimeItemCell(style: cellStyle)
    }
}

// 然后可以单独设置样式
TimeItemCell.appearance().titleColor = .purple

最佳实践建议

  1. 样式管理:将样式设置代码集中管理,便于维护和主题切换
  2. 类型安全:尽可能使用自定义子类和枚举,避免硬编码字符串
  3. 模块化:将复杂表单拆分为多个方法或扩展
  4. 响应式设计:考虑不同屏幕尺寸和方向的布局
  5. 可访问性:确保样式设置不会影响辅助功能

结语

Sheeeeeeeeet 提供了强大的扩展能力,通过自定义子类和样式设置,开发者可以创建高度定制化的动作表单。本文介绍的高级技巧可以帮助你构建更复杂、更专业的用户界面组件。

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛靓璐Gifford

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值