菜鸟教程小白 发表于 2022-12-11 19:08:03

ios - 创建一个每秒运行的计时器,在第二个?


                                            <p><p>我有一个特定日期的倒计时。它看起来很不错,并且每秒更新一次以显示倒计时。这是我用来创建计时器的代码:</p>

<pre><code>func scheduleTimeLabelsUpdateTimer() {

    var components = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date())
    components.second! += 1

    let nextSecondDate = Calendar.current.date(from: components)!

    let timer = Timer(fireAt: nextSecondDate, interval: 1, target: self, selector: #selector(updateTimeLabels), userInfo: nil, repeats: true)

    RunLoop.main.add(timer, forMode: .commonModes)

}
</code></pre>

<p>但是,我希望它每秒更新一次,<em>第二次</em>,以便时钟在每一秒过去的同时更新。现在它从调用此方法开始每秒更新一次,该方法位于 <code>viewDidLoad()</code> 中。</p>

<p>例如,如果倒计时设置为午夜,我希望它在午夜准确地达到零。现在,它可能会在午夜后略微达到零,具体取决于用户打开此屏幕时的秒数。</p>

<p>编辑:这是向用户显示倒计时的方式。 <code>updateTimeLabels()</code> 只是根据距该日期的剩余时间设置每个标签的文本。我希望每个标签都能准确地每秒更新一次。这样倒计时将准确地按时“归零”。请注意,现在,秒数达到零,然后状态栏上的系统时钟更新。我希望这些同时发生。</p>

<p> <a href="/image/xn32U.gif" rel="noreferrer noopener nofollow"><img src="/image/xn32U.gif" alt="enter image description here"/></a> </p>

<p>这段代码是我几个月前在 Stack Overflow 上找到的,在 <code>updateTimeLabels()</code> 中调用它来计算剩余时间:</p>

<pre><code>public func timeOffset(from date: Date) -&gt; (days: Int, hours: Int, minutes: Int, seconds: Int) {
    // Number of seconds between times
    var delta = Double(self.seconds(from: date))

    // Calculate and subtract whole days
    let days = floor(delta / 86400)
    delta -= days * 86400

    // Caluclate and subtract whole hours
    let hours = floor(delta / 3600).truncatingRemainder(dividingBy: 24)
    delta -= hours * 3600

    // Calculate and subtract whole minutes
    let minutes = floor(delta / 60.0).truncatingRemainder(dividingBy: 60)
    delta -= minutes * 60

    // What&#39;s left is seconds
    let seconds = delta.truncatingRemainder(dividingBy: 60)

    return (Int(days), Int(hours), Int(minutes), Int(seconds))
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在您的代码中,您在初始化组件时从不使用纳秒</p>

<p>因此计时器将始终按到第二个轮数。</p>

<p>我在下面测试您的代码,在选择器 <a href="/image/eiwn6.png" rel="noreferrer noopener nofollow"><img src="/image/eiwn6.png" alt="enter image description here"/></a> 中打印 <code>Date()</code> </p>

<p>我不确定这是否是您所说的“命中零”,希望这会有所帮助</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 创建一个每秒运行的计时器,在第二个?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42547362/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42547362/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 创建一个每秒运行的计时器,在第二个?