dcb*_*nji 0 while-loop control-flow swift
我是编程新手,之前没有在我的应用程序中使用'while'语句.我有一个cocoapod动画圆圈计时器图形.完成动画后,它会将'didFinish'BOOL设置为'true'.我能够成功检查这个变量,但是当我告诉我的应用程序将我的一个按钮标签设置为特定文本'而'didFinish为false时,它会冻结我的应用程序.
//start the timer and change the label to "Reset"
else {
brewingTimer.start()
buttonSelect = 1
startTimer.setTitle("Reset", forState: .Selected)
//Start the circle counter graphic
circleCounterOuter.startWithSeconds(5)
circleCounterInner.startWithSeconds(2)
while circleCounterInner.didFinish == false {
startTimer.setTitle("Reset!", forState: .Selected)
}
startTimer.setTitle("Ok!", forState: .Selected)
}
Run Code Online (Sandbox Code Playgroud)
我点击"开始"的那一刻,应用程序冻结了.当我在while循环中为de-bugger设置一个断点时,应用程序似乎在我的语句中循环,因为我可以点击"跳过",因为它循环通过'while-loop'.但是当我解决问题时,我当然看不到我的动画在屏幕上运行,所以我不确定实际发生了什么导致模拟迟缓.
您的代码阻止了主线程.
这段代码忙着一遍又一遍地执行:
while circleCounterInner.didFinish == false {
startTimer.setTitle("Reset!", forState: .Selected)
}
Run Code Online (Sandbox Code Playgroud)
同时,主线程上的任何其他内容都无法执行任何操作(例如更新文本或响应触摸事件),因此您的应用似乎已被冻结.
尝试不同的模式.例如,startTimer在长时间运行的工作开始时设置标签.然后,以不同的方法在工作完成时更新按钮.
您可能希望了解委派和通知设计模式,这些模式通常用于此类情况.
| 归档时间: |
|
| 查看次数: |
498 次 |
| 最近记录: |