swift - 如何使用 UIColorPickerViewController 更改两件事的颜色
问题描述
我想用 UIColorPickerViewController 改变两种颜色(图标和背景)。
现在我可以更改图标的颜色。但我也需要改变背景的颜色。
但是,我不知道如何分离扩展部分。
目前,我的代码如下;
@IBAction func btnColorPickerPressed(_ sender: Any) {
let colorPickerVC = UIColorPickerViewController()
colorPickerVC.delegate = self
present(colorPickerVC, animated: true) {
}
}
@IBAction func bgColorPickerPressed(_ sender: Any) {
let colorPickerVC = UIColorPickerViewController()
colorPickerVC.delegate = self
present(colorPickerVC, animated: true) {
}
}
extension ViewController : UIColorPickerViewControllerDelegate {
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
showImage()
}
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
iconColor = viewController.selectedColor
}
}
解决方案
创建 2 个不同的UIColorPickerVieweController
并简单地检查哪个是哪个
@IBAction func btnColorPickerPressed(_ sender: Any) {
let colorPickerVC = UIColorPickerViewController()
colorPickerVC.delegate = self
colorPickerVC.view.tag = 1
present(colorPickerVC, animated: true) {
}
}
@IBAction func bgColorPickerPressed(_ sender: Any) {
let colorPickerVC = UIColorPickerViewController()
colorPickerVC.delegate = self
colorPickerVC.view.tag = 2
present(colorPickerVC, animated: true) {
}
}
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
if(viewController.view.tag == 1) {
iconColor = viewController.selectedColor
}
else if(viewController.view.tag == 2) {
backgroundColor = viewController.selectedColor
}
}
推荐阅读
- maven - Intellij 未显示本地库
- api - 将 api 作为窗口服务托管时出现 Net core 3.1 异常
- node.js - 在 Node js Express 应用程序中处理多个用户
- python - 对于多类分类的 RNN 序列,我可以使用什么损失函数?
- sockets - 如何在不同网络的计算机之间传输文件?
- python-3.x - Django 在同一字段上注释和应用不同的聚合函数
- google-play - 无法理解 Google Play 上的应用审核流程
- ffmpeg - 带有concat的ffmpeg:复制一些,重新编码其他,即选择性重新编码
- php - foreach 语句导致多文件上传中未定义的变量 fileNameToStore
- laravel-8 - 想要使用谷歌客户端 api 库和谷歌在 laravel 8 中进行距离和步长的运行会话吗?