0

I'm relatively new to SwiftUI and I want to update a wave function (which represents how full the coffee is) by changing the variable caffeineContent with a Button.

The value is changing if I print it but the Content-View stays the same and isn't updating. I thought about forcing to update the UI, but this solution does not seem right to me.

Info:

caffeineContent = 50 --> Coffee is full

caffeineContent = 0 --> Coffee is empty

var caffeineContent: Int = 10


struct ContentView: View {
    
    
    @State private var showModal = false
    @State private var percent = caffeineContent
    
    
    var body: some View {
        ZStack {
            
            Color(red: 0.5, green: 0.4, blue: 0.35).ignoresSafeArea()
    
     […]
                                        
                    CircleWaveView(percent: Int(caffeineContent)).padding(EdgeInsets(top: 90, leading: 65, bottom: 100, trailing: 79))
                        
              
                }
                
     […]
                Button(action: {
                    
                    caffeineContent = caffeineContent+5
                    print(caffeineContent)
                   
                }, label: {
                    Text("Add caffeine content").font(.largeTitle)

[…]

The coffee cup with the wave inside, which shall be updated

Bartek
  • 93
  • 10
  • 1
    This might be helpful https://stackoverflow.com/a/63412977/12299030, as well as https://stackoverflow.com/a/64347221/12299030. – Asperi Oct 28 '20 at 15:59

1 Answers1

0

Thank you @Asperi! This is a solution, I got now.

struct ContentView: View {
    

@State private var showModal = false
**@State private var percent: Int = 50**
    
[...]
           
                    
CircleWaveView(percent: **Int(self.percent)**)
.padding(EdgeInsets(top: 90, leading: 65, bottom: 100, trailing: 79))
                  
[...]     
                Button(action: {
                    **self.percent -= 5**
                    
                   
                }, label: {
                    Text("Add caffeine content").font(.largeTitle)
                })
[...]
Bartek
  • 93
  • 10