Post

Replies

Boosts

Views

Activity

Reply to Implement a timer for an activity.
I implemented the timer this way... Only thing not working is invalidating the timer inspite of doing it on the main thread (same thread as firing the timer) @IBAction func playOrPauseButtonPressed(_ sender: Any) { var timer: Timer? = nil timerInterval = habit?.timer if !play! { if (timer == nil) { timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in if self.secondsElapsed <= Int(self.timerInterval!) { self.secondsElapsed = self.secondsElapsed + 1 let progress = Float(self.secondsElapsed)/Float(self.timerInterval!) DispatchQueue.main.async { self.timerProgressView.setProgress(progress, animated: true) } } else { DispatchQueue.main.async { timer.invalidate() self.playOrPauseButton.imageView?.image = UIImage(named: "Play") } self.play = false self.secondsElapsed = 0 } } } DispatchQueue.main.async { self.playOrPauseButton.imageView?.image = UIImage(named: "Pause") timer?.fire() } play = true } else { DispatchQueue.main.async { self.playOrPauseButton.imageView?.image = UIImage(named: "Play") timer?.invalidate() } play = false } } Any help with that second part of invalidating the thread? I see the timer keeps firing and the progress view keeps updating.
Topic: UI Frameworks SubTopic: UIKit Tags:
Feb ’24
Reply to Implement a timer for an activity.
Unfortunately I cannot undo the above post marked as answered... Here is the correct solution with play, pause and stop... @IBAction func playOrPauseButtonPressed(_ sender: Any) { timerInterval = habit?.timer if !play! { if (timer == nil) { //make new timer timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in //let timer be fired until time interval if timer.isValid { self.secondsElapsed = self.secondsElapsed + 1 let progress = Float(self.secondsElapsed)/Float(self.timerInterval!) DispatchQueue.main.async() { self.timerProgressView.setProgress(progress, animated: true) } } else { DispatchQueue.main.async() { self.playOrPauseButton.imageView?.image = UIImage(named: "Play") } self.play = false } } } DispatchQueue.main.async() { self.timer?.fire() //when timer fires show pause button self.playOrPauseButton.imageView?.image = UIImage(named: "Pause") } play = true } else { DispatchQueue.main.async() { self.timer?.invalidate() self.timer = nil self.playOrPauseButton.imageView?.image = UIImage(named: "Play") } play = false } } @IBAction func stopButtonPressed(_ sender: Any) { DispatchQueue.main.async() { self.timer?.invalidate() self.timer = nil self.timerProgressView.setProgress(0, animated: true) self.playOrPauseButton.imageView?.image = UIImage(named: "Play") } play = false secondsElapsed = 0 } ``` If i set the timer as nil outside the dispatch block, things dont work. dunno why.
Topic: UI Frameworks SubTopic: UIKit Tags:
Feb ’24
Reply to Xcode 15 - Unable to install iOS 17 Simulator runtime
@Developer Tools Engineer For older versions like iOS 16.4 installation failed saying not enough disk space. I had nearly 60gb free. Now, I removed xcode and all cache completely and re-installed it from Mac App Store. For iOS 17 its the same error, 16.4 succeeded. Also, (devices) iPhone iOS 17 and iPad iPadOS 16.5 are recognised in Devices and Simulators but doesn't show up in Run destinations even after setting them to be shown 'Always'.
Sep ’23
Reply to Implement a timer for an activity.
I implemented the timer this way... Only thing not working is invalidating the timer inspite of doing it on the main thread (same thread as firing the timer) @IBAction func playOrPauseButtonPressed(_ sender: Any) { var timer: Timer? = nil timerInterval = habit?.timer if !play! { if (timer == nil) { timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in if self.secondsElapsed <= Int(self.timerInterval!) { self.secondsElapsed = self.secondsElapsed + 1 let progress = Float(self.secondsElapsed)/Float(self.timerInterval!) DispatchQueue.main.async { self.timerProgressView.setProgress(progress, animated: true) } } else { DispatchQueue.main.async { timer.invalidate() self.playOrPauseButton.imageView?.image = UIImage(named: "Play") } self.play = false self.secondsElapsed = 0 } } } DispatchQueue.main.async { self.playOrPauseButton.imageView?.image = UIImage(named: "Pause") timer?.fire() } play = true } else { DispatchQueue.main.async { self.playOrPauseButton.imageView?.image = UIImage(named: "Play") timer?.invalidate() } play = false } } Any help with that second part of invalidating the thread? I see the timer keeps firing and the progress view keeps updating.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Feb ’24
Reply to Implement a timer for an activity.
Unfortunately I cannot undo the above post marked as answered... Here is the correct solution with play, pause and stop... @IBAction func playOrPauseButtonPressed(_ sender: Any) { timerInterval = habit?.timer if !play! { if (timer == nil) { //make new timer timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in //let timer be fired until time interval if timer.isValid { self.secondsElapsed = self.secondsElapsed + 1 let progress = Float(self.secondsElapsed)/Float(self.timerInterval!) DispatchQueue.main.async() { self.timerProgressView.setProgress(progress, animated: true) } } else { DispatchQueue.main.async() { self.playOrPauseButton.imageView?.image = UIImage(named: "Play") } self.play = false } } } DispatchQueue.main.async() { self.timer?.fire() //when timer fires show pause button self.playOrPauseButton.imageView?.image = UIImage(named: "Pause") } play = true } else { DispatchQueue.main.async() { self.timer?.invalidate() self.timer = nil self.playOrPauseButton.imageView?.image = UIImage(named: "Play") } play = false } } @IBAction func stopButtonPressed(_ sender: Any) { DispatchQueue.main.async() { self.timer?.invalidate() self.timer = nil self.timerProgressView.setProgress(0, animated: true) self.playOrPauseButton.imageView?.image = UIImage(named: "Play") } play = false secondsElapsed = 0 } ``` If i set the timer as nil outside the dispatch block, things dont work. dunno why.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Feb ’24
Reply to Xcode 15 - Unable to install iOS 17 Simulator runtime
@Developer Tools Engineer For older versions like iOS 16.4 installation failed saying not enough disk space. I had nearly 60gb free. Now, I removed xcode and all cache completely and re-installed it from Mac App Store. For iOS 17 its the same error, 16.4 succeeded. Also, (devices) iPhone iOS 17 and iPad iPadOS 16.5 are recognised in Devices and Simulators but doesn't show up in Run destinations even after setting them to be shown 'Always'.
Replies
Boosts
Views
Activity
Sep ’23