gpt4 book ai didi

ios - 计时器无法正常工作,Swift

转载 作者:可可西里 更新时间:2023-11-01 00:56:48 25 4
gpt4 key购买 nike

我正在尝试让计时器计数向上我按住按钮的秒数。(按住以录制视频)它无法正常工作......有时它会跳过秒数& 有时只是卡住然后加速 catch ......我真的需要你的帮助来解决这个问题,我需要计时器尽可能流畅和恒定,谢谢。

//Timer Variables:
var recordingCounter : Timer!
var minutes: Int = 0
var seconds: Int = 0
var counterString : String = ""

func longTap(_ sender: UIGestureRecognizer){
if sender.state == .ended {
print("UIGestureRecognizerStateEnded")

recordingCounter.invalidate()

seconds = 0
minutes = 0

if self.timerView.alpha == 1.0 {
UIView.animate(withDuration: 0.2, animations: {

self.timerView.alpha = 0.0
self.timerView.isHidden = true
self.recordingCounter.invalidate()

self.seconds = 0
self.minutes = 0

self.counterString = "00:00"
self.countingLabel.text = self.counterString
})
}

return

}else if sender.state == .began {

timerView.isHidden = false
UIView.animate(withDuration: 0.2, animations: {

self.timerView.alpha = 1.0

})

print("UIGestureRecognizerStateBegan.")

recordingCounter = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
}
}

func updateCounter() {

seconds += 1
if seconds == 60 {

minutes += 1
seconds = 0

}

let secondsString = seconds > 9 ? "\(seconds)" : "0\(seconds)"
let minutesString = minutes > 9 ? "\(minutes)" : "0\(minutes)"

counterString = "\(minutesString):\(secondsString)"
countingLabel.text = counterString

}

顺便说一句,我同时运行我的自定义 AVFoundation 相机的 VideoPreviewLayer.. 可能会导致问题吗?

最佳答案

Timer 在与用户交互相同的 DispatchQueue 中工作(例如在 UITableView 中滚动,在 UIGestureRecognizer 或按住 UIbutton)。因此,当用户执行任何操作时,Timer 将卡住片刻。

您可以使用例如 AsyncTimer 来解决这个问题.

关于ios - 计时器无法正常工作,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45522878/

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