EFColorPicker 是一个纯 Swift 的轻量级 iOS 颜色选择器,受 MSColorPicker 启发。
链接
概述
iOS 颜色选择器组件,它能够让用户选择自定义颜色,关键特性如下:
- 支持 iPhone 和 iPad
- 自适应的用户界面
- 支持 RGB 和 HSB 两种颜色模式
- 比较完善的文档和注释
- 支持 iOS 8.0 (iPhone & iPad) 及更高版本
预览
iPhone | iPad | |
---|---|---|
示例
- 利用
git clone
命令下载本仓库; - 利用 cd 命令切换到 Example 目录下,执行
pod install
命令; - 随后打开
EFColorPicker.xcworkspace
编译即可。
或执行以下命令:
git clone git@github.com:EyreFree/EFColorPicker.git; cd EFColorPicker/Example; pod install; open EFColorPicker.xcworkspace
复制代码
环境
- iOS 8.0+
- Xcode 9.0+
- Swift 4.0+
安装
EFColorPicker 可以通过 CocoaPods 进行获取。只需要在你的 Podfile 中添加如下代码就能实现引入:
pod "EFColorPicker"
复制代码
使用
- 首先,需要导入 EFColorPicker 库:
import 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
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 调用:
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 | ||
---|---|---|---|
具体可参考示例程序。
- 最后,不要忘记调用的 ViewController 需要继承 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 的第一个版本从 MSColorPicker 转换而来,在此对 MSColorPicker 的作者 sgl0v 表示感谢!
作者
EyreFree, eyrefree@eyrefree.org
协议
EFQRCode 基于 MIT 协议进行分发和使用,更多信息参见协议文件。
如有任何知识产权、版权问题或理论错误,还请指正。
https://juejin.im/post/5a32179ff265da431a43359d
转载请注明原作者及以上信息。