[英]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.