Я настраиваю приложение для работы в качестве простого таймера, это просто приложение с одним представлением, с некоторыми функциями для каждой из различных длин.
При нажатии кнопки переменные обновляются, потому что яустановите их для печати, просто ярлык не обновляется.
Я пробовал несколько разных способов, чтобы попытаться поместить его в основной поток, но ни один из них не работал.
import UIKit
import AudioToolbox
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
var seconds = 0
var stateOfPotentialBurning = false
var shouldBeRunning = true
@IBOutlet weak var timeLeft: UILabel!
@IBOutlet weak var currentState: UILabel!
@IBAction func basicFish(_ sender: Any) {
basicFishTimer()
}
@IBAction func basicMeat(_ sender: Any) {
basicMeatTimer()
}
@IBAction func trophyFish(_ sender: Any) {
trophyFishTimer()
}
@IBAction func monsterMeat(_ sender: Any) {
monsterMeatTimer()
}
@IBAction func offStop(_ sender: Any) {
shouldBeRunning = false
seconds = 0
timeLeft.textColor = UIColor.black
currentState.textColor = UIColor.black
stateOfPotentialBurning = false
}
func basicFishTimer() {
print(1)
DispatchQueue.main.async() {
self.currentState.text = "Cooking"
}
seconds = 45
DispatchQueue.main.async() {
self.timeLeft.text = String(self.seconds)
}
print(2)
while (seconds > 0 && shouldBeRunning == true) {
seconds -= 1
DispatchQueue.main.async() {
self.timeLeft.text = String(self.seconds)
}
sleep(1)
print(seconds)
}
stateOfPotentialBurning = true
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
seconds = 0
print(3)
while stateOfPotentialBurning == true && shouldBeRunning == true{
DispatchQueue.main.async() {
self.currentState.text = "Done"
}
DispatchQueue.main.async() {
self.timeLeft.textColor = UIColor.red
}
seconds -= 1
if (seconds == 0) {
seconds = 0
DispatchQueue.main.async() {
self.currentState.text = "Burnt"
}
self.currentState.text = "Burnt"
DispatchQueue.main.async() {
self.currentState.textColor = UIColor.red
}
currentState.textColor = UIColor.red
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
stateOfPotentialBurning = false
shouldBeRunning = false
}
sleep(1)
print(4)
}
}
func basicMeatTimer() {
currentState.text = "Cooking"
seconds = 65
repeat {
seconds -= 1
timeLeft.text = "\(seconds)"
sleep(1)
} while seconds > 0 && shouldBeRunning == true
stateOfPotentialBurning = true
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
seconds = 0
while stateOfPotentialBurning == true && shouldBeRunning == true{
currentState.text = "Done"
timeLeft.textColor = UIColor.red
seconds -= 1
if (seconds == 0) {
seconds = 0
currentState.text = "Burnt"
currentState.textColor = UIColor.red
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
stateOfPotentialBurning = false
shouldBeRunning = false
}
sleep(1)
}
}
func trophyFishTimer() {
currentState.text = "Cooking"
seconds = 95
repeat {
seconds -= 1
timeLeft.text = "\(seconds)"
sleep(1)
} while seconds > 0 && shouldBeRunning == true
stateOfPotentialBurning = true
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
seconds = 0
while stateOfPotentialBurning == true && shouldBeRunning == true{
currentState.text = "Done"
timeLeft.textColor = UIColor.red
seconds -= 1
if (seconds == 0) {
seconds = 0
currentState.text = "Burnt"
currentState.textColor = UIColor.red
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
stateOfPotentialBurning = false
shouldBeRunning = false
}
sleep(1)
}
}
Работает, просто ярлыки не обновляются