继续之前的倒计时 - swift

countdown before proceeding - swift

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

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()
}

您可以使用 DispatchGroups。定时器开始时入群,定时器失效时退出

private var currentCountdownSeconds = 3
private var countdownTimer = Timer()
private let timerDispatchGroup = DispatchGroup() // Init DispatchGroup

private func performTimer() {

    if secondsToCountDown != 0 {
        print(secondsToCountDown)
        timerDispatchGroup.enter() // Enter DispatchGroup
        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()
        timerDispatchGroup.leave() // Leave DispatchGroup
    }
}

public func toggleMovieRecording() {
    performTimer() // I guess it should be performTimer instead of handleTimer?
    timerDispatchGroup.notify(queue: .main) { videoCaptureLogic() } // Closure 
}

public func toggleCapturePhoto() {
    performTimer()
    timerDispatchGroup.notify(queue: .main) { 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")
    }
}

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

class Something {

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

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

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