gpt4 book ai didi

swift - 继续之前的倒计时 - swift

转载 作者:行者123 更新时间:2023-11-30 11:18:17 25 4
gpt4 key购买 nike

需要帮助制作一个倒计时器,直到其失效才能继续。它也不应该阻塞主线程。有什么建议吗?

private var currentCountdownSeconds = 3
private var countdownTimer = Timer()

private func performTimer() {

if secondsToCountDown != 0 {
print(secondsToCountDown)
countdownTimer = Timer(timeInterval: 1, target: self, selector: #selector(handleCountdown), userInfo: nil, repeats: true)
}

}

@objc private func handleCountdown() {
previewView.countdownLabel.text = "\(currentCountdownSeconds)"
currentCountdownSeconds -= 1
print(secondsToCountDown)
if secondsToCountDown == 0 {
countdownTimer.invalidate()
}
}

public func toggleMovieRecording() {
handleTimer()
videoCaptureLogic()
}

public func toggleCapturePhoto() {
handleTimer()
videoCaptureLogic()
}

最佳答案

如果您必须在多个地方使用倒计时,请尝试这个。

private var currentCountdownSeconds = 3
private var countdownTimer = Timer()

private func performTimer() {

if secondsToCountDown != 0 {
print(secondsToCountDown)
countdownTimer = Timer(timeInterval: 1, target: self, selector: #selector(handleCountdown), userInfo: nil, repeats: true)
}

}

@objc private func handleCountdown() {
previewView.countdownLabel.text = "\(currentCountdownSeconds)"
currentCountdownSeconds -= 1
print(secondsToCountDown)
if secondsToCountDown == 0 {
countdownTimer.invalidate()
NotificationCenter.default.post(notification: NSNotification.Name.init("TimeComplete")
}
}

现在,在您需要的任何类中实现通知的观察者。当倒计时完成并发布通知时,相应的选择器将处理该事件。

class Something {

init() {
NotificationCenter.default.addObserver(self, selector: #selector(timerCompleteAction), name: NSNotification.Name.init("TimerComplete"), object: nil)
}

@objc func timerCompleteAction() {
//Do necessary actions
}
}

Something 可以是具有视频捕获功能的类,在这种情况下,将该代码写入 timerCompleteAction 中。然后再次在进行音频捕获的类中,添加相同的观察者并添加选择器方法并执行音频捕获操作。

关于swift - 继续之前的倒计时 - swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51556472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com