UILabel не обновляется в цикле? - PullRequest
0 голосов
/ 09 мая 2019

Я настраиваю приложение для работы в качестве простого таймера, это просто приложение с одним представлением, с некоторыми функциями для каждой из различных длин.

При нажатии кнопки переменные обновляются, потому что яустановите их для печати, просто ярлык не обновляется.

Я пробовал несколько разных способов, чтобы попытаться поместить его в основной поток, но ни один из них не работал.

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)

        }



    }

Работает, просто ярлыки не обновляются

1 Ответ

3 голосов
/ 09 мая 2019

Не используйте бесконечные циклы while (или while running) в iOS.Это блокирует поток, в котором вы запускаете цикл.Ваша попытка обновить пользовательский интерфейс с помощью вызова DispatchQueue.main.async() не будет работать до тех пор, пока цикл не вернется, чего не происходит.

Как предполагает Исаак, используйте объект Timer.Это вызовет ваш код в нужное время и будет работать на переднем или фоновом потоке.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...