Is it possible to make it so that when the button is pressed, the variable can smoothly change its value from 0 to 1 within, for example, 0.01 seconds? This is required so that there are no clicks when changing the sound volume.
struct ContentView: View {
@State var volume:Float = 0
var body: some View {
VStack(spacing: 50) {
Text("\(volume)")
Button("1") {
volume = 1
}
Button("0") {
volume = 0
}
}
}
}
struct ContentView: View {
let timer = Timer.publish(every: 0.001, on: .main, in: .common).autoconnect()
@State var toggle = false
@State var count:CGFloat = 0
var body: some View {
VStack(spacing: 50) {
Button("ON") {
toggle = true
}
Button("OFF") {
toggle = false
}
Text("\(count)")
.font(.largeTitle)
}
.onReceive(timer, perform: { _ in
if toggle == true && count < 1 {
count += 0.01
} else if toggle == false && count > 0 {
count -= 0.01
}
})
}
}