简体   繁体   English

快速倒数计时器逻辑

[英]Swift countDown timer Logic

I'm creating a countdown timer by the day, hour and second. 我正在按天,小时和秒创建一个倒数计时器。 I'm currently storing them in a String array while also converting them into an Int ( using map() ) so that I can countdown/decrement when I make the values into a label. 我目前将它们存储在String数组中,同时还将它们转换为Int(使用map() ),以便在将值制成标签时可以倒数/递减。

The current output when printing to the logs is: ["[68168]", "[68188]", "[68243]", "[68281]"] 打印到日志时的当前输出为: ["[68168]", "[68188]", "[68243]", "[68281]"]

I'm having trouble forming the logic of a countDown function so that when seconds hits "0", 1 minute goes down, and after 59 minutes 1 hour is decremented, etc., until it hits "00:00:00". 我在形成countDown函数的逻辑时遇到了麻烦,因此当秒数达到“ 0”时,1分钟下降,而59分钟后,1小时递减,依此类推,直到达到“ 00:00:00”。

In each index, I have the 3 values of [Hour, Minute, Second]. 在每个索引中,我具有[小时,分钟,秒]的3个值。 But I have several indexes. 但是我有几个索引。

Currently I'm taking the Hour * 3600, the minute * 60 + the second. 目前,我正在使用小时* 3600,分钟* 60 +秒。 My question is how I can divide this up and break it down in the countDown function... 我的问题是如何在countDown函数中将其分解并分解...

Here is my code: 这是我的代码:

   //Here I'm querying and converting the objects into Hour, Minute, Seconds.

 var createdAt = object.createdAt
            if createdAt != nil {

                let calendar = NSCalendar.currentCalendar()
                let comps = calendar.components([.Hour, .Minute, .Second], fromDate: createdAt as NSDate!)
                let hour = comps.hour * 3600
                let minute = comps.minute * 60
                let seconds = comps.second
                let intArray = [hour, minute, seconds]

                //Now i'm appending to the String array below. 

                self.timeCreatedString.append("\(intArray)")

               var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDown"), userInfo: nil, repeats: true)


            }


             func countDown() {

                     //completely stuck as to how to decrement/divide up the values so that I can display it as: 23:49:01 (Hours, minutes, seconds). 

                 }

How can I form it so that each element within each index countdowns separately? 如何形成表格,以便每个索引中的每个元素分别倒数? ex: "23: 59: 59" 例如:“ 23:59:59”

PS, this is being built on a TableViewController. PS,这是建立在TableViewController上的。 I want to print this to a label that is in a cell View Controller in the cellForRowAtIndexPath method 我想将此打印到cellForRowAtIndexPath方法的单元格视图控制器中的标签

Keep your countdown time in seconds in a single variable called countdown : 在一个称为countdown变量中,以秒为单位保持倒数时间:

var countdown = 7202 // two hours and two seconds

When it comes time to display it, break it into hours , minutes , and seconds and use the String(format:) constructor to format it: 需要显示时,将其分成hoursminutesseconds然后使用String(format:)构造函数对其进行格式化:

// loop 5 times to demo output    
for _ in 1...5 {
    let hours = countdown / 3600
    let minsec = countdown % 3600
    let minutes = minsec / 60
    let seconds = minsec % 60
    print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))
    countdown--
}

Output: 输出:

02:00:02
02:00:01
02:00:00
01:59:59
01:59:58

If you have more than one timer, keep them in an array: 如果您有多个计时器,请将它们放在一个数组中:

// Array holding 5 different countdown timers referred to as
// timers[0], timers[1], timers[2], timers[3] and timers[4]
var timers = [59, 61, 1000, 3661, 12345]

for i in 0 ..< times.count {
    let hours = timers[i] / 3600
    let minsec = timers[i] % 3600
    let minutes = minsec / 60
    let seconds = minsec % 60
    print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))
}

Output: 输出:

00:00:59
00:01:01
00:16:40
01:01:01
03:25:45

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM