在 Swift 中使用静态函数通过 @State 或 @ObservableObject 动态改变颜色
我有一个 SwiftUI 视图,顶部有一些文本字段,我正在尝试动态更改这些文本字段的颜色。到目前为止,我发现的最佳选择是保留一个 @State
变量,该变量保存与每个文本字段的颜色相对应的颜色数组。但是,我试图从另一个类更新这个 @State
,这似乎不可能。
为了尝试解决这个问题,我正在考虑使用 ObservableObject
和 @Published
数组,其工作方式与前面提到的颜色数组相同:该类可以执行以下操作颜色的改变会在某个地方包含这个数组,并且会用新的颜色更新数组;然后,View
将具有一个 @ObservedObject
,并且每个 Text
将引用 @ObservedObject
。
但这是我的问题:我希望能够更改颜色的函数是静态的。 Swift 不会让我创建一个 @Published static var,因此这个静态函数无法更改作为 @ObservedObject
焦点的数组。是否有另一种方法可以设置类似于 @State
或 @ObservedObject
的东西,它允许我动态更新颜色,也可以从静态函数访问?
这是我希望设置的一个粗略想法:
保存文本元素的视图:
struct TextView: View {
@ObservedObject var colorChange = ColorChangingClass()
var body: some View {
HStack {
Text("Text Field 1")
.padding()
.background(Color.gray)
.foregroundColor(colorChange.colorArray[0])
.cornerRadius(5.0)
Text("Text Field 2")
.padding()
.background(Color.gray)
.foregroundColor(colorChange.colorArray[1])
.cornerRadius(5.0)
Text("Text Field 3")
.padding()
.background(Color.gray)
.foregroundColor(colorChange.colorArray[2])
.cornerRadius(5.0)
}
}
}
具有改变颜色的静态函数的类:
class ColorChangingClass: ObservableObject {
static func changingColors() {
// I want to be able to change the colors from here
}
// Swift won't let me make this static
@Published var colorArray: [Color] = [Color.blue, Color.red, Color.green]
}
colorArray 的放置是灵活的,只要我可以从这个静态访问它函数,这似乎是我能找到的最接近解决方案的东西,但显然它不太有效。
I have a SwiftUI view with a few text fields at the top, and I am trying to dynamically change the color of these. The best option I have found so far is to keep a @State
variable that holds an array of colors corresponding to the colors of each of the text fields. However, I am trying to update this @State
from another class, which doesn't seem possible.
To try to fix this, I was looking at using an ObservableObject
with a @Published
array that works the same way as the previously mentioned array of colors: the class that would do the changing of colors would have this array within it somewhere, and would update the array with new colors; the View
would then have an @ObservedObject
, and each Text
would reference the @ObservedObject
.
Here is my issue though: the function that I want to be able to change the colors is static. Swift will not let me make a @Published static var
, so this static function can't change the array that is the focus of the @ObservedObject
. Is there another way to set up something similar to @State
or @ObservedObject
that would allow me to dynamically update colors, that is also accessible from a static function?
Here is a rough idea of what I am hoping to set up:
The view that holds the Text elements:
struct TextView: View {
@ObservedObject var colorChange = ColorChangingClass()
var body: some View {
HStack {
Text("Text Field 1")
.padding()
.background(Color.gray)
.foregroundColor(colorChange.colorArray[0])
.cornerRadius(5.0)
Text("Text Field 2")
.padding()
.background(Color.gray)
.foregroundColor(colorChange.colorArray[1])
.cornerRadius(5.0)
Text("Text Field 3")
.padding()
.background(Color.gray)
.foregroundColor(colorChange.colorArray[2])
.cornerRadius(5.0)
}
}
}
The class with the static function that alters the colors:
class ColorChangingClass: ObservableObject {
static func changingColors() {
// I want to be able to change the colors from here
}
// Swift won't let me make this static
@Published var colorArray: [Color] = [Color.blue, Color.red, Color.green]
}
The placement of the colorArray is flexible so long as I can access it from this static function, this just seemed to be the closest thing to a solution that I could find, but obviously it doesn't quite work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单答案中动态更改颜色:这是不可能的。
简单原因:
@publanded
仅在实例上工作,而不是静态的缺少上下文,我只是想从不同的视图中更改颜色。如果那是将函数作为静态实现的原因,则有一个解决方案。
将您的
colorChangingClass
实例化最高视图,将其用作@StateObject
。通过.environmentObject()
将其传递给子女视图。如果您提供更多上下文,则可能还有其他可能的解决方案。
Simple answer: this is not possible.
Simple reason:
@Published
works only on instance var and not staticAs there is missing context I just asume you want to change the colors from different views. If thats the reason for implementing the function as static there is a workaround.
Instantiate your
ColorChangingClass
at the top most View that uses it as a@StateObject
. Hand it down to the child views via.environmentObject()
.If you provide more context there could be other possible solutions too.