分支 | 状态 | 覆盖率 |
---|---|---|
master (v0.8.3) |
||
develop |
演示 | 目录 |
---|---|
![]() |
📄 要求
- iOS 9.0 或更高版本(iOS 11.0+ 用于拖放)
- Xcode 10.2 或更高版本
- Swift 5 或更高版本
💻 安装
Cocoapods
PDColorPicker
通过 CocoaPods 提供。
如果尚未如此操作,请在应用的根目录下运行 pod setup
要安装 PDColorPicker
,只需将以下行添加到 Podfile 中
pod 'PDColorPicker'
此行应添加到应用程序的目标中,使其看起来像这样
use_frameworks!
target 'TARGET_NAME' do
pod 'PDColorPicker'
# other pods...
end
旧版 Swift 版本
如果项目没有为 Swift 4.2 构建,请从
swift-3.2
、swift-4.1
或swift-4.2
分支安装pod 'PDColorPicker', :git => 'https://github.com/pdil/PDColorPicker.git', :branch => 'swift-4.2'请注意,此构建可能不包括
PDColorPicker
的所有最新功能。
在项目根目录中从终端运行以下命令来构建 CocoaPods 框架
$ pod install
打开新创建的 .xcworkspace
文件并构建项目以使 PDColorPicker
可用。
注意:请始终在 .xcworkspace
文件中工作,而不是在 .xcodeproj
文件中,否则 Xcode 将无法定位依赖项,并且无法访问 PDColorPicker
。
Carthage
PDColorPicker
可通过 Carthage 获得。
如果您尚未安装 Carthage,请使用 Homebrew 安装。
$ brew update
$ brew install carthage
在项目根目录中创建一个包含以下行的 Cartfile(如果您已经有了 Cartfile,请添加此行)
github "pdil/PDColorPicker"
旧版 Swift 版本
如果项目没有为 Swift 4.2 构建,请从
swift-3.2
、swift-4.1
或swift-4.2
分支安装github "pdil/PDColorPicker" "swift-4.2"
请注意,此构建可能不包括
PDColorPicker
的最新功能。
在根项目目录的 Terminal 中运行以下命令以构建 Carthage 框架
$ carthage update
这将构建位于 Carthage/build
目录中的框架。
最后,将框架添加到您的项目中
- 在 Xcode 中,选择项目导航器中的项目(蓝色图标)。
- 在上侧栏打开“通用”选项卡。
- 将
PDColorPicker.framework
从Carthage/build
目录拖动到“嵌入的二进制文件”部分。
手动(不推荐)
- 下载 PDColorPicker/Classes 中的
.swift
文件并将它们添加到您的项目中。 - 将这些文件添加到项目中的适当目标。
- 像平常一样导入
PDColorPicker
。
📝 使用
import UIKit
import PDColorPicker // 1.
class MyViewController: UIViewController, Dimmable {
// ...
func presentColorPicker() {
// 2.
let colorPickerVC = PDColorPickerViewController(initialColor: .blue, tintColor: .black)
// 3.
colorPickerVC.completion = {
[weak self] newColor in
self?.undim() // 7.
guard let color = newColor else {
print("The user tapped cancel, no color was selected.")
return
}
print("A new color was selected! HSBA: \(String(describing: color))")
}
// 4.
dim() // see Dimmable documentation for extra options
// 5.
present(colorPickerVC, animated: true)
}
// ...
}
- 导入
PDColorPicker
框架。 - 实例化一个新的
PDColorPickerViewController
。 - 设置颜色选择器的完成处理程序,指示呈现视图控制器应如何处理颜色结果。 注意:这也可以在
PDColorPickerViewController
初始化器中设置。 - 实现
Dimmable
协议并暗淡呈现视图控制器(可选但强烈推荐)。 - 将颜色选择器作为模态视图控制器呈现。
- 使用颜色选择器选择颜色。当点击 保存 或 取消 时,初始器中指定的完成处理程序将自动提供新的颜色。如果用户点击取消,则返回
nil
。 - 确保在完成处理程序被调用后将视图恢复。
附加功能
为了在暗淡呈现视图控制器时实现白色状态栏,请在您的 Info.plist
中将 UIViewControllerBasedStatusBarAppearance
设置为 YES
。
您还可以将此处的内容复制到 plist 文件中
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
📲 拖放
从 iOS 11.0 或更高版本的项目中支持拖放。
请参阅 PDColorReceiverExample
以了解如何消费拖入您应用程序的颜色示例。 PDColorPicker
的拖放实现暴露了一个基本的 UIColor
,以便不一定要导入 PDColorPicker
的应用程序读取。
当然,如果目标应用程序导入了 PDColorPicker
,它将可以访问便捷的 PDColor
类,该类可以使用接收到的 UIColor
创建实例。
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
session.loadObjects(ofClass: UIColor.self) {
guard let color = $0.first as? UIColor else { return }
let pdColor = PDColor(color: color)
print(pdColor.hex)
}
}
🎨 PDColorPicker
在其他应用中
以下是使用 PDColorPicker 让用户选择颜色的应用程序列表
🌮 FoodEase🎨 Paint Shapes
如果您的应用程序在使用 PDColorPicker,告诉我,我将将其添加到此列表(包括用于在列表中表示您应用程序的emoji、应用程序名称以及网址)!
🙋♂️ 作者
Paolo Di Lorenzo
⚖️ 许可证
PDColorPicker 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。