简体   繁体   English

从 SwiftUI 拾色器获取价值

[英]Get Value from SwiftUI Color Picker

iOS 14 shipped with native color pickers for UIKit ( UIColorWell ), AppKit ( NSColorWell ) and SwiftUI ( ColorPicker ). iOS 14 随附用于 UIKit ( UIColorWell )、AppKit ( NSColorWell ) 和 SwiftUI ( ColorPicker ) 的原生颜色选择器。

While the color wells have a property selectedColor that exposes the selected UIColor / NSColor values, the SwiftUI ColorPicker takes a Binding<Color> .虽然颜色井有一个属性selectedColor可以公开选定的UIColor / NSColor值,但 SwiftUI ColorPicker采用Binding<Color> However, I can't find any information how to get anything useful from the Color value.但是,我找不到任何信息如何从Color值中获得任何有用的信息。

Creating a Color from a UIColor is easy using Color.init(_: UIColor) but there does not seem to be a way to get a UIColor from an existing Color or to extract the color components (eg the RGB values similar to the UIColor.getRed(_:green:blue:alpha:) method).使用Color.init(_: UIColor)UIColor创建Color很容易,但似乎没有办法从现有Color中获取UIColor或提取颜色分量(例如,类似于UIColor.getRed(_:green:blue:alpha:)方法)。

How do I get the underlying color values from a SwiftUI ColorPicker ?如何从 SwiftUI ColorPicker获取基础颜色值?

In majority of cases you use Color in SwiftUI directly, ie it is not needed to extract anything from it.在大多数情况下,您直接在 SwiftUI 中使用Color ,即不需要从中提取任何内容。

Anyway if it is needed to have UIColor SwiftUI 2.0 provides new API for that无论如何,如果需要UIColor SwiftUI 2.0 为此提供新的 API

 extension UIColor { @available(iOS 14.0, tvOS 14.0, watchOS 7.0, *) @available(OSX, unavailable) public convenience init(_ color: Color) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM