继续前的倒计时-swift

继续前的倒计时-swift,swift,timer,block,completion,Swift,Timer,Block,Completion,需要帮助来创建一个倒计时计时器,该计时器将一直等待,直到其失效后才能继续。它也不应该阻塞主线程。有什么建议吗 private var currentCountdownSeconds = 3 private var countdownTimer = Timer() private func performTimer() { if secondsToCountDown != 0 { print(secondsToCountDown) countdownTi

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

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")
    }
}
现在在您需要的任何类中实现通知的观察者。当倒计时完成并发布通知时,相应的选择器将处理事件

class Something {

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

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

Something
可以是具有视频捕获的类,在这种情况下,请在
timercompleaction
中编写该代码。然后,在有音频捕获的类中,添加相同的观察者和选择器方法,并执行音频捕获操作。

那么,代码中有什么问题吗?为什么不只给出
countDownTimer=Timer(时间间隔:currentCountdownSeconds,目标:self,选择器:#选择器(handleCountdown),userInfo:nil,repeats:true)
并在调用选择器后使其无效?@AhmadF代码执行时没有错误,但是,我不知道如何使代码等到计时器失效后再继续执行另一行代码。@Rakeshashstri,我相信您的解决方案会使选择器每3秒调用一次。我需要每秒更新一次标签,使其看起来像一个倒计时计时器。只需在倒计时计时器后的
handleCountdown()
方法中添加另一行代码。invalidate()有趣的是,我不熟悉DispatchGroup。让我来探讨一下。现在,您的解决方案给了我(
无法使用类型为“(()->())”
)的参数列表调用“notify”error.hmm,这很有趣。我知道你的解决办法了。给我一点时间试试看。如果你被卡住了,请告诉我。