Hi All,
I have made a rather basic 30 second countdown timer which I will use for pool tournaments as a shot clock. I have working functions for start/stop/extension buttons etc which all work fine however I'm also trying to have a separate timer countdown from 30 minutes or 1 hour which would be changeable like my current timer with the slider bar and this one would be the match clock but unsure how to add a separate timer that doesn't interfere with the shot clock timer already working.
for reference, I'm a beginner at using Xcode :)
Here is where I am currently.
Thanks in advance.
// // ViewController.swift // Shot Clock 5 // // Created by Marcus Allen on 16/03/2022. //
import UIKit import AVFoundation import SwiftUI
class ViewController: UIViewController, UITextFieldDelegate{
var seconds = 30
var timer = Timer()
var audioPlayer = AVAudioPlayer()
var isPlaying = false
@IBOutlet weak var label: UILabel!
@IBOutlet weak var sliderOutlet: UISlider!
@IBAction func slider(_ sender: UISlider)
{
seconds = Int(sender.value)
label.text = String(seconds)
}
@IBAction func name2(_ sender: UITextField) {
sender.resignFirstResponder()
}
@IBAction func name1(_ sender: UITextField) {
sender.resignFirstResponder()
}
@IBAction func score2(_ sender: UITextField) {
sender.resignFirstResponder() }
@IBAction func score1(_ sender: UITextField) {
sender.resignFirstResponder()
}
@IBOutlet weak var startOutlet: UIButton!
@IBAction func start(_ sender: Any)
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.counter), userInfo: nil, repeats: true)
sliderOutlet.isHidden = true
startOutlet.isHidden = true
}
@objc func counter()
{
seconds -= 1
label.text = String(seconds) + ""
if (seconds == 0)
{
timer.invalidate()
}
if (seconds == 5)
{
do {
}
sliderOutlet.isHidden = false
startOutlet.isHidden = false
audioPlayer.play()
}
}
@IBAction func EXT2(_ sender: UIButton) {seconds += 15 }
@IBAction func ext2pressed(_ sender: UIButton) {
sender.backgroundColor = sender.backgroundColor == UIColor.red ? UIColor.clear : UIColor.red }
@IBAction func EXT1(_ sender: UIButton) {seconds += 15 }
@IBAction func ext1pressed(_ sender: UIButton) {
sender.backgroundColor = sender.backgroundColor == UIColor.red ? UIColor.clear : UIColor.red
}
@IBAction func ext1resetaudio(_ sender: Any) {
audioPlayer.stop()
audioPlayer.currentTime = 0
isPlaying = false }
@IBAction func ext2resetaudio(_ sender: Any) {
audioPlayer.stop()
audioPlayer.currentTime = 0
isPlaying = false }
@IBOutlet weak var stopOutlet: UIButton!
@IBAction func stop(_ sender: Any)
{
timer.invalidate()
seconds = 30
sliderOutlet.setValue(30 , animated: true)
label.text = "30"
audioPlayer.stop()
sliderOutlet.isHidden = false
startOutlet.isHidden = false
}
@IBAction func stopresetaudio(_ sender: Any) {
audioPlayer.stop()
audioPlayer.currentTime = 0
isPlaying = false }
override func viewDidLoad()
{
super.viewDidLoad()
do
{
let audioPath = Bundle.main.path(forResource: "3", ofType: ".mp3")
try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
}
catch {
//ERROR
}
}
}