Reputation: 167
Hello guys i need some help.
Im trying to create a countdown watch which can count down from hours, minuts and seconds. Right now im only able to create the countdown for seconds but i want the application to be able to update the seconds, minutes and hours when i "slide" with the slider. I find it very hard to update the label correctly and add "hours and minutes" to the application. Can someone please help me figuring the logic out.
This is the code i have written so far and it works fine only with seconds.. I also added a audio file which will play in the end as you can see in the code.
class ViewController: UIViewController {
var secondsCount = 30;
var timer = Timer()
var audioPlayer = AVAudioPlayer()
@IBOutlet weak var label: UILabel!
@IBOutlet weak var labelmin: UILabel!
// Slideren som slider tid for sal 1
@IBOutlet weak var sliderOutlet: UISlider!
@IBAction func slider(_ sender: UISlider)
{
//Live changes the numbers
secondsCount = Int(sender.value)
label.text = String(secondsCount) + " Seconds"
}
//Start button
@IBOutlet weak var startOutlet: UIButton!
@IBAction func start(_ sender: Any)
{
//Nederstående kode aktiverer funktionen counter()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.counter), userInfo: nil, repeats: true)
sliderOutlet.isHidden = true
startOutlet.isHidden = true
}
//Counter function
func counter() {
secondsCount -= 1
label.text = String(secondsCount) + " Seconds"
if (secondsCount == 0)
{
timer.invalidate()
audioPlayer.play()
}
}
//Stop button
@IBOutlet weak var stopOutlet: UIButton!
@IBAction func stop(_ sender: Any)
{
timer.invalidate()
secondsCount = 30
sliderOutlet.setValue(30, animated: true)
label.text = "30 Seconds"
audioPlayer.stop()
sliderOutlet.isHidden = false
startOutlet.isHidden = false
}
// viewDidLoad
override func viewDidLoad()
{
super.viewDidLoad()
do
{
let audioPath = Bundle.main.path(forResource: "1", ofType: ".mp3")
try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
}
catch
{
//ERROR
}
}
Upvotes: 0
Views: 457
Reputation: 77637
Here is one way to convert number of seconds into a formatted hours, minutes, seconds string:
func hmsFromSecondsFormatted(seconds: Int) -> String {
let h = seconds / 3600
let m = (seconds % 3600) / 60
let s = seconds % 60
var newText = ""
if h > 0 {
newText += "\(h)"
if h == 1 {
newText += " hour, "
} else {
newText += " hours, "
}
}
if m > 0 || h > 0 {
newText += "\(m)"
if m == 1 {
newText += " minute, "
} else {
newText += " minutes, "
}
}
newText += "\(s)"
if s == 1 {
newText += " second"
} else {
newText += " seconds"
}
return newText
}
Then you could use it like this:
label.text = hmsFromSecondsFormatted(secondsCount)
The multiple if
conditions give you two things:
a result of singular / plural time component names (so you get "1 second" instead of "1 seconds"), and
returns only the necessary time components. So, 45 seconds is returned as "45 seconds" instead of "0 hours, 0 minutes, 45 seconds"
In your actual app, you probably will also use localized strings for the time component names.
Hope that helps :)
Upvotes: 1