swift 后的计时器倒计时跳过秒并暂停
Timer countdown in swift skipping seconds and pausing
我正在为正在更新的应用制作倒计时组件。我终于让它工作了,但是当它在我的模拟器上或者当我 运行 通过我的 phone 它时,它停滞不前并跳过几秒钟,有时会冻结我的屏幕并且不允许我更改视图等。这是我下面的代码。我很好奇为什么会这样。
提前致谢!
class CountdownViewController: UIViewController {
@IBOutlet weak var days: UILabel!
@IBOutlet weak var hours: UILabel!
@IBOutlet weak var minutes: UILabel!
@IBOutlet weak var seconds: UILabel!
var timer:NSTimer!
func reloadData(){
self.viewDidLoad()
}
override func viewDidLoad() {
super.viewDidLoad()
self.timer = NSTimer(timeInterval: 0.5, target: self, selector: Selector("reloadData"), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
self.canDisplayBannerAds = true
// here we set the current date
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay | .CalendarUnitSecond, fromDate: date)
let hour = components.hour
let minute = components.minute
let month = components.month
let year = components.year
let day = components.day
let second = components.second
let currentDate = calendar.dateFromComponents(components)
// here we set the due date. When the timer is supposed to finish
let userCalendar = NSCalendar.currentCalendar()
let electionDate = NSDateComponents()
electionDate.year = 2016
electionDate.month = 11
electionDate.day = 08
electionDate.hour = 00
electionDate.minute = 00
electionDate.second = 00
let electionDay = userCalendar.dateFromComponents(electionDate)!
// Here we compare the two dates
electionDay.timeIntervalSinceDate(currentDate!)
let dayCalendarUnit: NSCalendarUnit = (.CalendarUnitDay | .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond)
//here we change the seconds to hours,minutes and days
let electionDayDifference = userCalendar.components(dayCalendarUnit, fromDate: currentDate!, toDate: electionDay,options: nil)
//finally, here we set the variable to our remaining time
var daysLeft = electionDayDifference.day
var hoursLeft = electionDayDifference.hour
var minutesLeft = electionDayDifference.minute
var secondsLeft = electionDayDifference.second
days.text = String(daysLeft)
hours.text = String(hoursLeft)
minutes.text = String(minutesLeft)
seconds.text = String(secondsLeft)
}
您每 0.5 秒向 RunLoop 添加越来越多的计时器。而且它们会同时被触发,随着时间的推移会导致性能下降。您需要将 // here we set the current date
注释下方的所有代码移动到 reloadData
函数并从那里删除 self.viewDidLoad()
,一切都应该没问题。您的计时器将只安排一次,并且每 0.5 秒重复调用一次 reloadData
(因为您使用 repeats: true
参数创建它)。
不要调用 viewDidLoad()
它是一种委托方法,由视图控制器调用(一次)并指定用于设置一次。
将您的代码放入自定义函数中,在计时器触发后执行。
不必一次又一次地创建计时器,将参数 repeats
设置为 true
会定期调用选择器。
我正在为正在更新的应用制作倒计时组件。我终于让它工作了,但是当它在我的模拟器上或者当我 运行 通过我的 phone 它时,它停滞不前并跳过几秒钟,有时会冻结我的屏幕并且不允许我更改视图等。这是我下面的代码。我很好奇为什么会这样。
提前致谢!
class CountdownViewController: UIViewController {
@IBOutlet weak var days: UILabel!
@IBOutlet weak var hours: UILabel!
@IBOutlet weak var minutes: UILabel!
@IBOutlet weak var seconds: UILabel!
var timer:NSTimer!
func reloadData(){
self.viewDidLoad()
}
override func viewDidLoad() {
super.viewDidLoad()
self.timer = NSTimer(timeInterval: 0.5, target: self, selector: Selector("reloadData"), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
self.canDisplayBannerAds = true
// here we set the current date
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay | .CalendarUnitSecond, fromDate: date)
let hour = components.hour
let minute = components.minute
let month = components.month
let year = components.year
let day = components.day
let second = components.second
let currentDate = calendar.dateFromComponents(components)
// here we set the due date. When the timer is supposed to finish
let userCalendar = NSCalendar.currentCalendar()
let electionDate = NSDateComponents()
electionDate.year = 2016
electionDate.month = 11
electionDate.day = 08
electionDate.hour = 00
electionDate.minute = 00
electionDate.second = 00
let electionDay = userCalendar.dateFromComponents(electionDate)!
// Here we compare the two dates
electionDay.timeIntervalSinceDate(currentDate!)
let dayCalendarUnit: NSCalendarUnit = (.CalendarUnitDay | .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond)
//here we change the seconds to hours,minutes and days
let electionDayDifference = userCalendar.components(dayCalendarUnit, fromDate: currentDate!, toDate: electionDay,options: nil)
//finally, here we set the variable to our remaining time
var daysLeft = electionDayDifference.day
var hoursLeft = electionDayDifference.hour
var minutesLeft = electionDayDifference.minute
var secondsLeft = electionDayDifference.second
days.text = String(daysLeft)
hours.text = String(hoursLeft)
minutes.text = String(minutesLeft)
seconds.text = String(secondsLeft)
}
您每 0.5 秒向 RunLoop 添加越来越多的计时器。而且它们会同时被触发,随着时间的推移会导致性能下降。您需要将 // here we set the current date
注释下方的所有代码移动到 reloadData
函数并从那里删除 self.viewDidLoad()
,一切都应该没问题。您的计时器将只安排一次,并且每 0.5 秒重复调用一次 reloadData
(因为您使用 repeats: true
参数创建它)。
不要调用 viewDidLoad()
它是一种委托方法,由视图控制器调用(一次)并指定用于设置一次。
将您的代码放入自定义函数中,在计时器触发后执行。
不必一次又一次地创建计时器,将参数 repeats
设置为 true
会定期调用选择器。