Hello! I have a maze game that I have just made, and the code works, the only thing I don't understand is that whenever I play and make a big maze, like 27x27 or bigger, it runs super slow and has a big lag every time i try to move. Does anyone know what I can do to lessen the lag? I know that it has a ton of views to compute when the maze is big, but most of the views are in Lazy Stacks so they shouldn't be causing as much of a problem as they are. Here's my code, I'd really appreciate it if someone had a good solution to my problem
import SwiftUI
struct ContentView: View {
@StateObject var data: DataBase
@StateObject var trafficControl: TrafficControl
var body: some View {
ZStack{
Group{
LazyVStack{
ForEach(1...data.size, id: \.self) { index in
LazyHStack {
ForEach(1...data.size, id: \.self) { box in
Rectangle().fill(Color.gray).frame(width: 90, height: 90, alignment: .center).scaledToFill().padding(-4)
}
}
}
}
LazyVStack{
ForEach(1...(data.size), id: \.self) { index in
LazyHStack {
ForEach(1...(data.size + 1), id: \.self) { box in
if data.vert[((index.self - 1) * (data.size + 1)) + box.self] == 1 {
Rectangle().fill(Color.black).frame(width: 6, height: 92, alignment: .center).scaledToFill().padding(.trailing, 38).padding(.leading, 38).padding(.top, -5).padding(.bottom, -5)
}
else {
Rectangle().fill(Color.gray).frame(width: 6, height: 92, alignment: .center).scaledToFill().padding(.trailing, 38).padding(.leading, 38).padding(.top, -5).padding(.bottom, -5).opacity(0)
}
}
}
}
}
LazyHStack{
ForEach(1...data.size, id: \.self) { index2 in
LazyVStack {
ForEach(1...(data.size + 1), id: \.self) { box2 in
if data.horz[((index2.self - 1) * (data.size + 1)) + box2.self] == 1 {
Rectangle().fill(Color.black).frame(width: 96, height: 6, alignment: .center).scaledToFill().padding(.trailing, -7).padding(.leading, -7).padding(.top, 38).padding(.bottom, 38)
}
else {
Rectangle().fill(Color.gray).frame(width: 96, height: 6, alignment: .center).scaledToFill().padding(.trailing, -7).padding(.leading, -7).padding(.top, 38).padding(.bottom, 38).opacity(0)
}
}
}
}
}
Rectangle().fill(Color.white).frame(width: 60, height: 60, alignment: .center).scaledToFill()
}.offset(x: CGFloat(data.ex), y: CGFloat(data.why)).animation(.linear(duration: 0.3), value: data.ex).animation(.linear(duration: 0.3), value: data.why).frame(width: 300, height: 300, alignment: .center)
Circle().fill(Color.black).frame(width: 20, height: 20, alignment: .center).scaledToFill()
Rectangle().fill(Color.green).frame(width: 1000, height: 1000, alignment: .center).offset(x: 590, y: 0)
Rectangle().fill(Color.green).frame(width: 1000, height: 1000, alignment: .center).offset(x: 0, y: 590)
Rectangle().fill(Color.green).frame(width: 1000, height: 1000, alignment: .center).offset(x: -590, y: 0)
Rectangle().fill(Color.green).frame(width: 1000, height: 1000, alignment: .center).offset(x: 0, y: -590)
HStack{
Text("corx: \(data.corx)")
Text("cory: \(data.cory)")
}.offset(x: 0, y: -200)
Text("Size: \(data.size)").offset(x: 0, y: -250)
Text("Back to menu").padding().background(Color.black).offset(x: 0, y: -300).onTapGesture {
trafficControl.currentPage = .page1
}
}.gesture(DragGesture(minimumDistance: 40)
.onEnded { endedGesture in
if (endedGesture.location.x - endedGesture.startLocation.x) > 0 {
if (endedGesture.location.y - endedGesture.startLocation.y) > (endedGesture.location.x - endedGesture.startLocation.x) {
data.MoveDown()
}
else if (endedGesture.startLocation.y - endedGesture.location.y) > (endedGesture.location.x - endedGesture.startLocation.x) {
data.MoveUp()
}
else {
data.MoveRight()
}
}
else if (endedGesture.startLocation.x - endedGesture.location.x) > 0 {
if (endedGesture.location.y - endedGesture.startLocation.y) > (endedGesture.startLocation.x - endedGesture.location.x) {
data.MoveDown()
}
else if (endedGesture.startLocation.y - endedGesture.location.y) > (endedGesture.startLocation.x - endedGesture.location.x) {
data.MoveUp()
}
else {
data.MoveLeft()
}
}
else if (endedGesture.startLocation.y - endedGesture.location.y) > (endedGesture.location.y - endedGesture.startLocation.y) {
data.MoveUp()
}
else {
data.MoveRight()
}
data.corx = -data.ex/90
data.cory = data.why/90
}
)}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(data: DataBase(), trafficControl: TrafficControl())
}
}