Reputation: 127
I'm trying to make a text
box that changes to a random one from a list
when a button
is pressed.
All im really trying to change I the background
color
, but then I decided I wanted a gradient
instead. It was working fine when I only had colors
in the backgrounds
list
, but now when I try to make a gradient
var
, so I don't have to put each gradient
in the list
, I keep getting the same error
.
Ive tried doing this all kinds of different ways and can't seem to get it to work anyway I try it. I feel like there has to be a way though.
import SwiftUI
struct ContentView: View {
lazy var drinkGradient = [LinearGradient(gradient: Gradient(colors: [Color("drinkcard1"), Color("drinkcard2")]), startPoint: .bottom, endPoint: .top)]
lazy var truthGradient = [LinearGradient(gradient: Gradient(colors: [Color("truthcard"), Color("truthcard")]), startPoint: .bottom, endPoint: .top)]
lazy var dareGradient = [LinearGradient(gradient: Gradient(colors: [Color("darecard"), Color("darecard")]), startPoint: .bottom, endPoint: .top)]
@State private var backgrounds = [truthGradient[0], dareGradient[0], truthGradient[0], dareGradient[0], truthGradient[0], dareGradient[0], drinkGradient[0]]
@State private var text = [String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Drink!!")]
@State private var foregrounds = [Color.black, Color.white]
@State private var number = [0]
var body: some View {
ZStack{
//Background
Rectangle()
.foregroundColor(Color("background"))
.edgesIgnoringSafeArea(.all)
//All content
VStack {
//Banner
HStack {
Text("Truth, Dare or Drink")
.font(.largeTitle)
.foregroundColor(.white)
.padding(.bottom, 30)
.padding(.top, 80)
.frame(minWidth: 0, maxWidth: .infinity)
}
.frame(minWidth: 0, maxWidth: .infinity)
.edgesIgnoringSafeArea(.all)
.background(Color("banner"))
.shadow(radius: 20)
Spacer()
Text(text[number[0]])
.font(.title)
.foregroundColor(foregrounds[0])
.padding(.all, 75)
.background(backgrounds[number[0]])
.cornerRadius(15)
.shadow(radius: 10)
.padding(.all)
Button(action: {
self.number[0] = Int.random(in:
0...self.text.count - 1)
if self.number[0] == 0{
}
}) {
Text("Spin")
.font(.title)
.padding(.vertical, 15)
.padding(.horizontal, 80)
.foregroundColor(.black)
.background(Color("button"))
.cornerRadius(25)
.shadow(radius: 15)
.padding(.all)
}
Spacer()
}.edgesIgnoringSafeArea(.all)
}.edgesIgnoringSafeArea(.all)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Upvotes: 0
Views: 59
Reputation: 8091
one solution could be to do the variables in a class like so:
class Colors {
static var drinkGradient = [LinearGradient(gradient: Gradient(colors: [Color("drinkcard1"), Color("drinkcard2")]), startPoint: .bottom, endPoint: .top)]
static var truthGradient = [LinearGradient(gradient: Gradient(colors: [Color("truthcard"), Color("truthcard")]), startPoint: .bottom, endPoint: .top)]
static var dareGradient = [LinearGradient(gradient: Gradient(colors: [Color("darecard"), Color("darecard")]), startPoint: .bottom, endPoint: .top)]
}
struct ContentView: View {
@State private var backgrounds = [Colors.truthGradient[0], Colors.dareGradient[0], Colors.truthGradient[0], Colors.dareGradient[0], Colors.truthGradient[0], Colors.dareGradient[0], Colors.drinkGradient[0]]
@State private var text = [String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Drink!!")]
@State private var foregrounds = [Color.black, Color.white]
@State private var number = [0]
var body: some View {
ZStack{
//Background
Rectangle()
.foregroundColor(Color("background"))
.edgesIgnoringSafeArea(.all)
//All content
VStack {
//Banner
HStack {
Text("Truth, Dare or Drink")
.font(.largeTitle)
.foregroundColor(.white)
.padding(.bottom, 30)
.padding(.top, 80)
.frame(minWidth: 0, maxWidth: .infinity)
}
.frame(minWidth: 0, maxWidth: .infinity)
.edgesIgnoringSafeArea(.all)
.background(Color("banner"))
.shadow(radius: 20)
Spacer()
Text(text[0])
.font(.title)
.foregroundColor(foregrounds[0])
.padding(.all, 75)
.background(backgrounds[0])
.cornerRadius(15)
.shadow(radius: 10)
.padding(.all)
Button(action: {
self.number[0] = Int.random(in:
0...self.text.count - 1)
if self.number[0] == 0{
}
}) {
Text("Spin")
.font(.title)
.padding(.vertical, 15)
.padding(.horizontal, 80)
.foregroundColor(.black)
.background(Color("button"))
.cornerRadius(25)
.shadow(radius: 15)
.padding(.all)
}
Spacer()
}.edgesIgnoringSafeArea(.all)
}.edgesIgnoringSafeArea(.all)
}
}
Upvotes: 2