EFColorPicker - 一个纯 Swift 的轻量级 iOS 颜色选择器

EFColorPicker 是一个纯 Swift 的轻量级 iOS 颜色选择器,受 MSColorPicker 启发。git

English Introductiongithub

连接

github.com/EyreFree/EF…swift

概述

iOS 颜色选择器组件,它可以让用户选择自定义颜色,关键特性以下:bash

  • 支持 iPhone 和 iPad
  • 自适应的用户界面
  • 支持 RGB 和 HSB 两种颜色模式
  • 比较完善的文档和注释
  • 支持 iOS 8.0 (iPhone & iPad) 及更高版本

预览

iPhone iPad

示例

  1. 利用 git clone 命令下载本仓库;
  2. 利用 cd 命令切换到 Example 目录下,执行 pod install 命令;
  3. 随后打开 EFColorPicker.xcworkspace 编译便可。

或执行如下命令:ide

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 中添加以下代码就能实现引入:post

pod "EFColorPicker"
复制代码

使用

  1. 首先,须要导入 EFColorPicker 库:
import EFColorPicker
复制代码
  1. 接下来,能够经过纯代码调用:
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 调用:ui

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
	    }
	}
}
复制代码

你能够经过修改 EFColorSelectionViewControllerisColorTextFieldHidden 属性来控制颜色编辑框的可见性,效果以下:spa

isColorTextFieldHidden: true isColorTextFieldHidden: false

具体可参考示例程序。debug

  1. 最后,不要忘记调用的 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 表示感谢!3d

做者

EyreFree, eyrefree@eyrefree.org

协议

EFQRCode 基于 MIT 协议进行分发和使用,更多信息参见协议文件。


若有任何知识产权、版权问题或理论错误,还请指正。 https://juejin.im/post/5a32179ff265da431a43359d 转载请注明原做者及以上信息。

相关文章
相关标签/搜索