TelegramColorPicker
使用 Swift & UIKit 构建的简洁的 telegram 风格颜色选择器
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。要使用 CocoaPods 将 TelegramColorPicker 集成到您的项目中,请在您的 Podfile
中指定它
pod 'TelegramColorPicker', '~> 1.3.1'
之后,在您的终端中使用 pod install
命令。
Carthage
Carthage 是一个分散式的依赖管理器,它构建您的依赖项并为您提供二进制框架。要使用 Carthage 将 TelegramColorPicker 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "IrelDev/TelegramColorPicker"
之后,在您的终端中使用 carthage update
命令。
Swift 包管理器
Swift 包管理器是一种自动化 Swift 代码分发工具,集成了 swift
编译器。要使用 Swift 包管理器将 TelegramColorPicker 集成到你的 Xcode 项目中,请在你的 Package
中指定它。
dependencies: [
.package(url: "https://github.com/IrelDev/TelegramColorPicker.git", .upToNextMinor(from: "1.3.0"))
]
手动安装
如果你不使用任何依赖管理器,可以通过下载所需发布版本并将 Sources
目录复制到你的项目中,手动将 TelegramColorPicker 集成到你的项目中。
警告
直接从 master 分支下下载的源代码可能不稳定,请从 发布页面 下载源代码。
使用方法
编程方法
创建 TelegramColorPicker
实例,并将其添加为子视图。
let frame = CGRect(x: .zero, y: .zero, width: 400, height: 250)
let colorPicker = TelegramColorPicker(frame: frame)
view.addSubview(colorPicker)
Storyboard 添加
从对象库中拖动 UIView
对象,并在身份检查器中将 TelegramColorPicker
设置为自定义类。为 TelegramColorPicker
类型创建并连接 @IBOutlet。
SwiftUI
创建一个符合 UIViewRepresentable
协议的结构体
struct TelegramColorPickerRepresentable: UIViewRepresentable {
func makeUIView(context: Context) -> TelegramColorPicker {
TelegramColorPicker()
}
func updateUIView(_ uiView: TelegramColorPicker, context: Context) { }
}
在您的 View
中使用它
var body: some View {
TelegramColorPickerRepresentable()
.frame(width: 400, height: 250, alignment: .center)
}
获取颜色更新
使用 getColorUpdate
函数与您的自定义闭包一起使用,并且别忘了使用 [weak self]
来避免强引用循环。
getColorUpdate { [weak self] (_, color) in
guard let newColor = color.newValue, let hexadecimalColor = newColor.toHex() else { return }
self?.textLabel.text = hexadecimalColor
}
示例
此存储库包含示例,您可以查看 TelegramColorPicker 是如何用于更改 QRCode 前景颜色的。
许可证
TelegramColorPicker 在 MIT 许可证下可用,有关更多信息,请参阅 LICENSE 文件。