This question already has an answer here:
这个问题在这里已有答案:
- How can I make a countdown timer like in a music player? 2 answers
- 如何在音乐播放器中制作倒数计时器? 2个答案
I'm trying to make an app that tell us the rest of time from the present time till one hour later.
我正在尝试制作一个应用程序,告诉我们从现在到一小时后的剩余时间。
This is the code but now it only has a function that tell us the countdown time by decreasing one second from the present time.
这是代码,但现在它只有一个函数,通过从当前时间减少一秒来告诉我们倒计时时间。
I'm thinking that I haven't definite the definition of the "cnt" so that's why I'm thinking it doesn't work.
我想我还没有确定“cnt”的定义,所以这就是为什么我认为它不起作用。
Can somebody tell me the reason and a solution?
有人能告诉我原因和解决方案吗?
import UIKit
class ViewController: UIViewController {
var cnt : Int = 0
var timer : NSTimer!//NSTimerというデフォルト機能から引っ張る
var myInt:Int = 0
override func viewDidLoad() {
let myDate: NSDate = NSDate()
let myCalendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let myComponents = myCalendar.components([.Year, .Hour, .Minute, .Second],
fromDate: myDate) // myDate、すなわちNSDateから要素として引っ張り出してる
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "onUpdate:", userInfo: nil, repeats: true)//カウントダウンのインターバル
timer.fire()
var myStr: String = "\(myComponents.hour)"
myStr += "\(myComponents.minute)"
myStr += "\(myComponents.second)"
myInt = Int(myStr)! // toInt()がSwift2より無効になったようです。myInt=Str(my components,hour,minute,second)=現時刻
}
func onUpdate(timer : NSTimer){
cnt += 1//cnt+1=cnt,
let count = myInt - cnt //残り時間=現在時刻ー現在時刻に1時間足した時刻
print(count) // println()は、Swift2よりDeprecatedになりました。
}
}
1 个解决方案
#1
0
It is difficult to understand what you're asking, but I will do my best.
很难理解你在问什么,但我会尽我所能。
In your viewDidLoad
method, you're setting myInt
to the integer representation of myStr
. If the time is 18:30:50, myInt
will be equal to 183050
. That is not an appropriate representation of the time. Time is base 60, integers are base 10, for one thing. If you want to represent time as a single number, you can use timeIntervalSinceDate
, or timeIntervalSinceReferenceDate
or timeIntervalSince1970
to get the NSTimeInterval
(ie. fractional seconds) representation of the date relative to a certain epoch either of your choosing or one built into Foundation.
在viewDidLoad方法中,您将myInt设置为myStr的整数表示形式。如果时间是18:30:50,myInt将等于183050.这不是时间的恰当表示。一般情况下,时间是60,整数是10。如果要将时间表示为单个数字,可以使用timeIntervalSinceDate或timeIntervalSinceReferenceDate或timeIntervalSince1970来获取相对于您选择的某个时期或基础内置的某个时期的日期的NSTimeInterval(即小数秒)表示。
Subtracting 1 from myInt each time the timer fires isn't going to give you an indication of the time remaining.
每次计时器触发时从myInt中减1都不会给你一个剩余时间的指示。
Also, NSTimer
is not an accurate way to keep time. You should instead save the start date as a property and determine the time remaining based on timeIntervalSinceDate
此外,NSTimer并不是一种准确的时间保留方式。您应该将开始日期另存为属性,并根据timeIntervalSinceDate确定剩余时间
e.g.
例如
func onUpdate(timer : NSTimer){
let currentTime = NSDate()
let timeElapsed = currentTime.timeIntervalSinceDate(myDate)
println(timeElapsed)
}
If you want to show time elapsed in minutes, you can divide it by 60. You can look into NSDateComponentsFormatter to easily get a string representation of time intervals.
如果要显示以分钟为单位的时间,可以将其除以60.您可以查看NSDateComponentsFormatter以轻松获取时间间隔的字符串表示。
If you want the countdown to stop after an hour, then check for when timeElapsed
is over 3600.
如果您希望倒计时在一小时后停止,则检查timeElapsed何时超过3600。
If you want it to show a countdown from 1 hour, then subtract the timeElapsed
from 3600.
如果您希望它显示1小时的倒计时,则从3600减去timeElapsed。
#1
0
It is difficult to understand what you're asking, but I will do my best.
很难理解你在问什么,但我会尽我所能。
In your viewDidLoad
method, you're setting myInt
to the integer representation of myStr
. If the time is 18:30:50, myInt
will be equal to 183050
. That is not an appropriate representation of the time. Time is base 60, integers are base 10, for one thing. If you want to represent time as a single number, you can use timeIntervalSinceDate
, or timeIntervalSinceReferenceDate
or timeIntervalSince1970
to get the NSTimeInterval
(ie. fractional seconds) representation of the date relative to a certain epoch either of your choosing or one built into Foundation.
在viewDidLoad方法中,您将myInt设置为myStr的整数表示形式。如果时间是18:30:50,myInt将等于183050.这不是时间的恰当表示。一般情况下,时间是60,整数是10。如果要将时间表示为单个数字,可以使用timeIntervalSinceDate或timeIntervalSinceReferenceDate或timeIntervalSince1970来获取相对于您选择的某个时期或基础内置的某个时期的日期的NSTimeInterval(即小数秒)表示。
Subtracting 1 from myInt each time the timer fires isn't going to give you an indication of the time remaining.
每次计时器触发时从myInt中减1都不会给你一个剩余时间的指示。
Also, NSTimer
is not an accurate way to keep time. You should instead save the start date as a property and determine the time remaining based on timeIntervalSinceDate
此外,NSTimer并不是一种准确的时间保留方式。您应该将开始日期另存为属性,并根据timeIntervalSinceDate确定剩余时间
e.g.
例如
func onUpdate(timer : NSTimer){
let currentTime = NSDate()
let timeElapsed = currentTime.timeIntervalSinceDate(myDate)
println(timeElapsed)
}
If you want to show time elapsed in minutes, you can divide it by 60. You can look into NSDateComponentsFormatter to easily get a string representation of time intervals.
如果要显示以分钟为单位的时间,可以将其除以60.您可以查看NSDateComponentsFormatter以轻松获取时间间隔的字符串表示。
If you want the countdown to stop after an hour, then check for when timeElapsed
is over 3600.
如果您希望倒计时在一小时后停止,则检查timeElapsed何时超过3600。
If you want it to show a countdown from 1 hour, then subtract the timeElapsed
from 3600.
如果您希望它显示1小时的倒计时,则从3600减去timeElapsed。