EFColorPicker 是一个基于 Swift 的轻量级颜色选择器,灵感来自 MSColorPicker。
概述
适用于 iOS 的颜色选择器组件。允许用户通过颜色分量选择颜色。主要特点:
- 支持 iPhone 和 iPad
- 自适应用户界面
- 支持 RGB 和 HSB 颜色模式
- 文档齐全
- 兼容 iOS 8.0(iPhone 和 iPad)及以上版本
预览
iPhone | iPad | |
---|---|---|
![]() |
![]() |
![]() |
示例
要运行示例项目,先克隆仓库,然后在 Example 目录中运行 pod install
。
然后使用 Xcode 构建并运行 EFColorPicker.xcworkspace
,该示例演示了如何将 EFColorPicker 整合到您的项目中。
需求
版本 | 需求 |
---|---|
<5.0 | Xcode 10.0+ Swift 4.2+ iOS 8.0+ |
5.x | Xcode 10.2+ Swift 5.0+ iOS 8.0+ |
安装
EFColorPicker 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'EFColorPicker'
使用
- 首先,将 EFColorPicker 包含到您的项目中
import EFColorPicker
- 然后,我们可以使用纯代码调用 EFColorPicker
let colorSelectionController = EFColorSelectionViewController()
let navCtrl = UINavigationController(rootViewController: colorSelectionController)
navCtrl.navigationBar.backgroundColor = UIColor.white
navCtrl.navigationBar.isTranslucent = false
navCtrl.modalPresentationStyle = UIModalPresentationStyle.popover
navCtrl.popoverPresentationController?.delegate = self
navCtrl.popoverPresentationController?.sourceView = sender
navCtrl.popoverPresentationController?.sourceRect = sender.bounds
navCtrl.preferredContentSize = colorSelectionController.view.systemLayoutSizeFitting(
UILayoutFittingCompressedSize
)
colorSelectionController.delegate = self
colorSelectionController.color = self.view.backgroundColor ?? UIColor.white
colorSelectionController.setMode(mode: EFColorSelectionMode.all)
if UIUserInterfaceSizeClass.compact == self.traitCollection.horizontalSizeClass {
let doneBtn: UIBarButtonItem = UIBarButtonItem(
title: NSLocalizedString("Done", comment: ""),
style: UIBarButtonItemStyle.done,
target: self,
action: #selector(ef_dismissViewController(sender:))
)
colorSelectionController.navigationItem.rightBarButtonItem = doneBtn
}
self.present(navCtrl, animated: true, completion: nil)
同时我们也可以在 Storyboard 中使用 EFColorPicker
if "showPopover" == segue.identifier {
guard let destNav: UINavigationController = segue.destination as? UINavigationController else {
return
}
if let size = destNav.visibleViewController?.view.systemLayoutSizeFitting(UILayoutFittingCompressedSize) {
destNav.preferredContentSize = size
}
destNav.popoverPresentationController?.delegate = self
if let colorSelectionController = destNav.visibleViewController as? EFColorSelectionViewController {
colorSelectionController.delegate = self
colorSelectionController.color = self.view.backgroundColor ?? UIColor.white
if UIUserInterfaceSizeClass.compact == self.traitCollection.horizontalSizeClass {
let doneBtn: UIBarButtonItem = UIBarButtonItem(
title: NSLocalizedString("Done", comment: ""),
style: UIBarButtonItemStyle.done,
target: self,
action: #selector(ef_dismissViewController(sender:))
)
colorSelectionController.navigationItem.rightBarButtonItem = doneBtn
}
}
}
您可以通过更改 EFColorSelectionViewController
的 isColorTextFieldHidden
属性来控制颜色文字字段的可见性,例如
isColorTextFieldHidden: true | isColorTextFieldHidden: false | ||
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
更多详情请参考演示。
- 最后但同样重要的是,您应该实现
EFColorSelectionViewControllerDelegate
以感知颜色变化
// MARK:- EFColorSelectionViewControllerDelegate
func colorViewController(colorViewCntroller: EFColorSelectionViewController, didChangeColor color: UIColor) {
self.view.backgroundColor = color
// TODO: You can do something here when color changed.
print("New color: " + color.debugDescription)
}
使用 EFColorPicker 的应用
|
|
|
作者
EyreFree, [email protected]
许可证
EFColorPicker 在MIT许可证下可用。更多信息请参见LICENSE文件。