EzKit
是一套基于 EzView
的自定义 UIKit 组件,EzView
是可选择的带有高亮功能的 UIView 子类。所有交互都是动画和代理的,具有高度的可定制选项。需要一个有两个标签和两个图像的按钮吗?我有!UIKit 中的内置动画复选框?易如反掌!
Recording-EzKit.mov
易于使用且功能强大的 UIView 子类。它会响应用户的长按,并允许高亮和选择操作,同时具有动画效果。
Recording-EzView.mov
使用 EzViewStyle
或 EzViewConfiguration
中的一个函数来配置 EzView
。您可以选择任意一种方法。
EzViewStyle
配置枚举提供了默认行为,配置选项较少,但更容易实现。
ezView.configure(style: .ez(tintColor: .systemBlue))
查看所有可能的病例以适合您所需的行为。例如,.selectionOnly
配置样式禁用高亮动画,仅保留选择动画。
EzViewConfiguration
配置类提供了所有可能的配置属性,因此您可以定制 EzView
以满足特定需要。您不需要提供所有初始化属性 - 它们将采用默认值,这些默认值在 EzConstants.swift
文件中定义。
// state of EzView while not selected
let normalState = EzViewState(borderColor: .clear,
backgroundColor: .systemGray)
// state of EzView while selected
let selectedState = EzViewState(borderColor: .systemMint,
backgroundColor: .systemMint.withAlphaComponent(0.3),
cornerRadius: 8)
ezView.configure(configuration: EzViewConfiguration(normalState: normalState,
selectedState: selectedState,
animationDuration: 0.3,
highlightScale: 0.95,
selectionScale: 1.05))
您会注意到所有配置初始化器和函数中的大多数属性都有默认值。这样可以简化配置过程,并使事情能够轻松启动,可能只需要几行代码。但我仍然鼓励您深入了解所有选项并尝试它们。
EzView
提供了闭包和代理函数,可用于控制反转 - 检测选择、高亮和动画状态变化。
ezView.onSelection = { [weak self] isSelected in
print("EzView has been selected?", isSelected)
}
ezView.onHighlight = { [weak self] isHighlighted in
print("EzView has been highlighted?", isHighlighted)
}
ezView.onAnimationStateChange = { [weak self] state in
print("EzView animation state:", state)
}
如果您更喜欢使用代理,EzView
也有这个功能!让您的所需类成为代理(例如 UIViewController
或视图模型)并实现 EzViewDelegate
函数。
class MainViewController: UIViewController, EzViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
ezView.delegate = self
}
func didSelect(_ ezView: EzView, isSelected: Bool) {
print("EzViewDelegate isSelected:", isSelected)
}
func didHighlight(_ ezView: EzView, isHighlighted: Bool) {
print("EzViewDelegate isHighlighted:", isHighlighted)
}
func didChangeAnimationState(_ ezView: EzView, state: EzAnimationState) {
print("EzView is animating selection?", state.isAnimatingSelection)
print("EzView is animating highlighting?", state.isAnimatingHighlight)
print("EzView is animating cancel highlight?", state.isAnimatingCancelHighlight)
}
}
乍一看,EzButton
将像普通的 UIButton
一样运行,但功能更多!这就是 EzView
真正发挥作用的地方。EzButton
是一个 EzView
子类,其中包含两个标签和两个图像作为子视图,组织方式类似于 UIButton
。然而,由于 EzView
的实现,此按钮完全可动画,并提供自定义选项!
Recording-EzButton.mov
与 EzView
类似,你有多种方式来配置 EzButton
。几乎所有属性都有默认值,如果您只想让things out of the box 。
ezButton.configure(button: .ez(),
titleText: "I am a button",
textColor: .white)
如果您想分别自定义标签和图像,请使用它们的设置函数。
ezButton.setupSubtitleLabel(text: "(with a subtitle)", textColor: .white)
ezButton.setupRightImageView(image: UIImage(systemName: "sun.max.fill"))
ezButton.setupLeftImageView(image: UIImage(systemName: "heart.fill"),
tintColor: .systemRed)
由于 EzButton
是 EzView
的子类,如果您希望获得更多选项(而不仅仅是 EzButtonStyle
所提供的内容),您也可以使用之前显示的配置。再次,玩玩吧!
使用闭包/代理函数来捕获点击和突出显示,您就准备就绪了!
EzCheckbox
是您可以使用 EzView
选择状态的另一个示例。只需定义您希望应用于每个状态的图像,提供一个 EzViewStyle
并开始触摸!
录制-EzCheckbox.mov
ezCheckbox.configure(style: .ez(tintColor: .systemGreen),
text: "I am a checkbox!",
textColor: .systemGreen,
normalStateImage: UIImage(systemName: "circle")!,
selectedStateImage: UIImage(systemName: "checkmark.circle")!)
使用闭包/代理函数来捕获点击和突出显示,您就准备就绪了!
欢迎提供反馈。我希望您能充分利用这个小库。享受吧!
EzKit 在 MIT 许可下发布。有关详细信息,请参阅 LICENSE。