Home > Enterprise >  Swiftui Reset Scrollview content and Foreach loop with a button
Swiftui Reset Scrollview content and Foreach loop with a button

Time:09-12

Is there any way to reset the scrollview or total app view including foreach loop with a reset button. I have following code which draws rectangles. Add amount and rotate. I want to reset all with a button click. Please help. This is a playground project.

import SwiftUI

struct ContentView: View {
    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible())
    ]    
    @State private var Amout = 10
    @State private var angle = 0.0
    var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 15) {
                ForEach(0..<20) { number in
                    
                    Rectangle()
                        .foregroundColor(Color.red)
                        .frame(width: 50, height: 80)
                        .cornerRadius(10)
                        .shadow(color: .black, radius: 5, x: 2, y: 2)
                        .rotationEffect(.degrees(angle))
                        .overlay(
                            Text("\(number)").foregroundColor(.white)
                        )
                        .onTapGesture {
                            Amout  = number
                            angle  = 5
                        }
                }
            }.padding(.all, 10)
            
            Text ("Total Amount is : \(Amout)")
                .font(.system(size: 20))
                .bold()
                .padding()
            
            Button {
                
                // Help needed here :-) 
                    
                }  label: {
                    Text("Reset")
                        .font(.system(size: 16))
                        .bold()
                        .foregroundColor(Color.blue)
                }
            
        }
    }
}

CodePudding user response:

import SwiftUI

struct test: View {
    
    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible())
    ]
    @State private var Amout = 10
    @State private var angle = 0.0
    
    @State var isVisible = false
    @State var resetButVisible = false
    @State var dealCardsVisible = true
    
    var body: some View {
        VStack {
            VStack {
                
                HStack {
                    
                    Rectangle()
                        .foregroundColor(Color.red)
                        .frame(width: 50, height: 80)
                        .cornerRadius(10)
                        .shadow(color: .black, radius: 5, x: 2, y: 2)
                        .padding()
                    
                    Button("Deal Cards", action: {
                        
                        dealCardsVisible.toggle()
                        isVisible.toggle()
                        resetButVisible.toggle()
                        
                    })
                    
                    Spacer()
                }.opacity(dealCardsVisible ? 1:0)
                
                LazyVGrid(columns: columns, spacing: 15) {
                            ForEach(0..<20) { number in
                                
                                Rectangle()
                                    .foregroundColor(Color.red)
                                    .frame(width: 50, height: 80)
                                    .cornerRadius(10)
                                    .shadow(color: .black, radius: 5, x: 2, y: 2)
                                    .rotationEffect(.degrees(angle))
                                    .overlay(
                                        Text("\(number)").foregroundColor(.white)
                                    )
                                    .onTapGesture {
                                        Amout  = number
                                        angle  = 5
                                    }
                            }
                }.padding(.all, 10)
                    .opacity(isVisible ? 1:0)
            }
                    
                    Text ("Total Amount is : \(Amout)")
                        .font(.system(size: 20))
                        .bold()
                        .padding()
                    
                    Button {
                        Amout = 10
                        angle = 0
                        
                        isVisible.toggle()
                        resetButVisible.toggle()
                        dealCardsVisible.toggle()
                        
                        }  label: {
                            Text("Reset")
                                .font(.system(size: 16))
                                .bold()
                                .foregroundColor(Color.blue)
                        }.opacity(resetButVisible ? 1:0)
                    
                }
            }
    }


struct test_Previews: PreviewProvider {
    static var previews: some View {
        test()
    }
}

its a little bit ugly but its work. I couldn't find what you want with lazyGrid. Maybe a little work that can seem good.

  • Related