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 }
// 处理表单提交
}
这个表单包含:
- 主标题
- 时间选择分段
- 单选时间选项
- 服务选择分段
- 多选服务选项
- 确定和取消按钮
自定义 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
最佳实践建议
- 样式管理:将样式设置代码集中管理,便于维护和主题切换
- 类型安全:尽可能使用自定义子类和枚举,避免硬编码字符串
- 模块化:将复杂表单拆分为多个方法或扩展
- 响应式设计:考虑不同屏幕尺寸和方向的布局
- 可访问性:确保样式设置不会影响辅助功能
结语
Sheeeeeeeeet 提供了强大的扩展能力,通过自定义子类和样式设置,开发者可以创建高度定制化的动作表单。本文介绍的高级技巧可以帮助你构建更复杂、更专业的用户界面组件。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考