I want to make a meditation app. There are two variables set by the user. After counting up from 1 to one variable, I want to count up the number from 1 to the other variable.
@AppStorage("exhaleTimer_Value") var exhaleTimerValue = 10
@AppStorage("inhaleTimer_Value") var inhaleTimerValue = 5
// Count up every second
timerHandler = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
// Called when the timer is executed
countdownTimer()
}
func countdownTimer() {
count += 1
I want it to look like this.
func breath() {
// Count up to the inhalation time set by the user
if count <= inhaleTimerValue {
// Count up to the time to inhale
} else {
// Since count exceeds the time set by the user, set it to 0.
count = 0
}
// This time, it counts up to the time you set to exhale.
if count <= exhaleTimerValue {
// Count up to the time to exhale
} else {
// Since count exceeds the time set by the user, set it to 0.
count = 0
}
// Count up to the time to inhale
}
What I want to know the most here is how to count up to one variable and then to another variable. If possible, I would be grateful if you could tell me about the process of repeating it.
I would be happy if you could answer.
Did you read the answer to the other thread ?
https://developer.apple.com/forums/thread/689769
What you could do here:
- create 2 timers
- when the first finishes, invalidate
- launch the second timer
var count1 = 0
var count2 = 0
var timer 1= Timer()
var timer 2= Timer()
@objc func fire1() {
count1 += 1
progressLabel1.text = " \(count)"
if count1 >= exhaleTimerValue {
timer1.invalidate()
timer2 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fire2), userInfo: nil, repeats: true)
}
}
@objc func fire2() {
count2 += 1
progressLabel2.text = " \(count)"
if count2>= inhaleTimerValue { timer2.invalidate()}
}
timer1 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fire1), userInfo: nil, repeats: true)
And this time, please answer to the thread to ack if you made it work or not.