I have a simple counter app for my personal use. It has 5 buttons; 1st adds one to the score, 2nd adds 2, 3rd adds 3, 4th resets the score, and the 5th takes 1 off the score. I have extracted all 5 to their own variables. This totals 5 variables. Is it possible to extract to one variable and configure it to satisfy all five buttons? I have attached the file for information. Any suggestions are appreciated. Thanks
import SwiftUI
struct Counter: View {
@State var score = 0
var body: some View {
NavigationView {
ZStack{
Rectangle()
.fill(Color.blue)
.ignoresSafeArea()
VStack{
Spacer()
Text(String(score))
.font(.system(size: 160, weight: .bold))
.fontWeight(.heavy)
.foregroundColor(Color.white)
.padding()
Spacer()
HStack{
addOneView
.padding()
addTwoView
.padding()
addThreeView
.padding()
}
.padding(40)
HStack {
resetView
minusOneView
}
.padding()
Spacer()
}
}
.navigationBarTitle("Simple Counter")
}
}
var addOneView: some View {
ZStack {
Button(action: {
addOne()
}) {
Text("+1")
.fontWeight(.bold)
.font(.title)
.frame(width: 100, height: 100)
.background(Color.purple)
.cornerRadius(40)
.foregroundColor(.black)
}
Circle()
.stroke(Color.black, lineWidth: 8)
.frame(width:100, height: 100)
}
}
var addTwoView: some View {
ZStack {
Button(action: {
addTwo()
}) {
Text("+2")
.fontWeight(.bold)
.font(.title)
.frame(width: 100, height: 100)
.background(Color.green)
.cornerRadius(40)
.foregroundColor(.black)
}
Circle()
.stroke(Color.black, lineWidth: 8)
.frame(width:100, height: 100)
}
}
var addThreeView: some View {
ZStack {
Button(action: {
addThree()
}) {
Text("+3")
.fontWeight(.bold)
.font(.title)
.frame(width: 100, height: 100)
.background(Color.yellow)
.cornerRadius(40)
.foregroundColor(.black)
}
Circle()
.stroke(Color.black, lineWidth: 8)
.frame(width:100, height: 100)
}
}
var resetView: some View {
ZStack {
Button(action: {
reset()
}) {
Text("Reset")
.fontWeight(.bold)
.font(.title)
.frame(width: 150, height: 80)
.background(Color.red)
.cornerRadius(40)
.foregroundColor(.black)
}
Capsule()
.stroke(Color.black, lineWidth: 8)
.frame(width:150, height: 80)
}
}
var minusOneView: some View {
ZStack {
Button(action: {
minusOne()
}) {
Text("-1")
.fontWeight(.bold)
.font(.title)
.frame(width: 150, height: 80)
.background(Color.gray)
.cornerRadius(40)
.foregroundColor(.black)
}
Capsule()
.stroke(Color.black, lineWidth: 8)
.frame(width:150, height: 80)
}
}
func addOne() {
score += 1
}
func addTwo() {
score += 2
}
func addThree() {
score += 3
}
func reset() {
score = 0
}
func minusOne() {
score -= 1
}
}
struct CounterView_Previews: PreviewProvider {
static var previews: some View {
Counter()
}
}