斯威夫特倒数计时器逻辑 [英] Swift countDown timer Logic

查看:271
本文介绍了斯威夫特倒数计时器逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建按天,小时和第二倒计时器。我目前正在将其储​​存在一个String数组,同时将其转换为一个int(使用地图()),这样我可以倒计时/递减,当我做的值成标签。

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.

打印到日志时,电流输出为: [[68168],[68188],[68243],[68281]]

The current output when printing to the logs is: ["[68168]", "[68188]", "[68243]", "[68281]"]

我无法形成倒计时功能的逻辑,这样当打秒0的1分钟停机,并在1小时减59分钟后等等,直到它击中00:00:00 。

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".

在每个索引,我的3个值[小时,分,秒。但是,我有几个指标。

In each index, I have the 3 values of [Hour, Minute, Second]. But I have several indexes.

目前我正在服用小时* 3600,分钟* 60 +第二。我的问题是我怎么能这样划分和分解的倒计时功能...

Currently I'm taking the Hour * 3600, the minute * 60 + the second. My question is how I can divide this up and break it down in the countDown function...

下面是我的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). 

                 }

我怎么可以形成它,这样单独每个索引倒计时中的每个元素?例如:23:59:59

How can I form it so that each element within each index countdowns separately? ex: "23: 59: 59"

PS,这是正在兴建一个的 TableViewController。的我想打印这一个标签,是一个的细胞的视图控制器中的的cellForRowAtIndexPath

PS, this is being built on a TableViewController. I want to print this to a label that is in a cell View Controller in the cellForRowAtIndexPath method

推荐答案

请在几秒钟内你的倒计时时间称为单变量倒计时

Keep your countdown time in seconds in a single variable called 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:

// 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--
}

输出:


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

输出:


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

这篇关于斯威夫特倒数计时器逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆