iOS 14 UIColorPicker 吸管工具未返回所选颜色
Posted
技术标签:
【中文标题】iOS 14 UIColorPicker 吸管工具未返回所选颜色【英文标题】:iOS 14 UIColorPicker eyedropper tool not returning selected color 【发布时间】:2021-11-21 14:28:57 【问题描述】:我目前正在尝试实现新的 ios 14 UIColorPicker。一切都很好,除了 UIColorPicker 的 eye dropper
功能。选择滴管并采样颜色后,默认行为应该是重新打开 UIColorPicker,并将您选择的颜色作为活动颜色。出于某种原因,这不会发生。这是我的实现,它非常标准,所以我不确定为什么滴管没有按预期运行。
当我按下一些 UI 元素时,这些函数作为选择器传递
@objc func pickColorSky(sender: UIControl)
presentColorPicker(tag: 1, sender: sender)
@objc func pickColorBackground(sender: UIControl)
presentColorPicker(tag: 2, sender: sender)
@objc func pickColorGround(sender: UIControl)
presentColorPicker(tag: 3, sender: sender)
这是呈现 UIColorPickerView 本身的函数
@objc private func presentColorPicker(tag: Int, sender: UIControl)
let vc = UIColorPickerViewController()
vc.supportsAlpha = false
vc.delegate = self
vc.view.tag = tag
vc.modalPresentationStyle = .popover
vc.popoverPresentationController?.sourceView = sender
vc.popoverPresentationController?.sourceRect = sender.bounds
self.present(vc, animated: true)
以及处理与颜色选择器交互的委托方法
extension myViewController: UIColorPickerViewControllerDelegate
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController)
guard let options = editor?.apiView?.getRenderingOptions() else return
if viewController.view.tag == 1
let newColor = getMobileApiColor(color: viewController.selectedColor)
options.skyColor = newColor
skyRow.color.backgroundColor = newColor.uiColor
else if viewController.view.tag == 2
let newColor = getMobileApiColor(color: viewController.selectedColor)
options.backgroundColor = newColor
backgroundRow.color.backgroundColor = newColor.uiColor
else if viewController.view.tag == 3
let newColor = getMobileApiColor(color: viewController.selectedColor)
options.groundColor = newColor
groundRow.color.backgroundColor = newColor.uiColor
editor?.modelView?.setNeedsDisplay()
【问题讨论】:
抱歉,Yogesh 是对的。我的无知... 【参考方案1】:试试这个方法colorPickerViewControllerDidSelectColor
extension ViewController: UIColorPickerViewControllerDelegate
// Called once you have finished picking the color.
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController)
self.view.backgroundColor = viewController.selectedColor
// Called on every color selection done in the picker.
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController)
self.view.backgroundColor = viewController.selectedColor
【讨论】:
我猜你是对的。 你好@benjamin-morris,如果这个答案对你有用,那么请接受这个答案,这样它也对其他人有帮助。 @YogeshPatel 这不是我的问题的解决方案,我不会将其标记为已接受。以上是关于iOS 14 UIColorPicker 吸管工具未返回所选颜色的主要内容,如果未能解决你的问题,请参考以下文章