I want a single button to change two color swatches to random colors. I have found code for changing one swatch. https://www.youtube.com/watch?v=iMr4rv4rk98&feature=emb_logo @State private var randomColor1 = UIColor(red: 0.8, green: 0.1, blue: 0.5, alpha: 1)
But get an error when I try to make two random colors "consecutive statements on a line must be separated by ';'"
Searching for two actions from one button, this seems to imply it's not possible but instead just "execute the closure": Is there a way to have a button run multiple functions in SwiftUI?
And here is my code:
@State private var randomColor1 = UIColor(red: 0.8, green: 0.1, blue: 0.5, alpha: 1)
@State private var randomColor2 = UIColor(red: 0.4, green: 0.0, blue: 0.8, alpha: 1)
var body: some View {
ZStack {
VStack{
HStack {
Rectangle()
.frame(width: 100, height: 200)
.foregroundColor(Color(randomColor1))
Rectangle()
.frame(width: 100, height: 200)
.foregroundColor(Color(randomColor2))
}
Button(action: {
self.randomColor1 = UIColor(
red:.random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1),
alpha: 1))
self.randomColor2 = UIColor(
red:.random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1),
alpha: 1)
)
}, label: {
Text("Make 2 Random colors")
})
}
Aucun commentaire:
Enregistrer un commentaire