【发布时间】:2017-08-27 15:59:54
【问题描述】:
我有一个sliderValueChange 函数可以更新UILabel 的文本。我希望它有一个时间限制,直到它清除标签的文本,但我也希望每当 UISlider 在“定时清除”之前的时间限制内移动时,取消并重新启动或延迟这个“定时清除”操作行动发生。
到目前为止,这是我所拥有的:
let task = DispatchWorkItem {
consoleLabel.text = ""
}
func volumeSliderValueChange(sender: UISlider) {
task.cancel()
let senderValue = String(format: "%.2f", sender.value)
consoleLabel.text = "Volume: \(senderValue)"
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3, execute: task)
}
显然,这种方法行不通,因为cancel() 显然无法逆转..(或者至少我不知道如何)。我也不知道如何在这个函数结束时开始一个新任务,如果函数被调用,它将被取消..
我是不是走错了路?有什么我忽略的东西来完成这项工作吗?
【问题讨论】:
-
我想要一个 NSTimer。如果要更改计时器周期,您仍然需要取消(无效)并重新安排计时器。但是,如果您确实想使用任务,是的,您需要创建并提交一个新任务。
标签: ios swift events delay cancellation