swift: 倒计时归零
swift: stop countdown timer at zero
我是 Swift 的新手 - 正在尝试为 iPhone/iPad 构建应用程序。希望您能够帮助我。
我想添加一个从 04:00 分钟倒数到 00:00 的计时器。然后它应该停在零并触发声音效果(我还没有尝试实现)。当您按下开始按钮时,倒计时开始(在我的代码中,startTimer 和 stopTimer 指的是同一个按钮;但是,按钮在开始时只被按下一次)。
计时器开始倒计时。它按计划将秒数转换为分钟数。但是,我的主要问题是我无法让倒计时停在零。它继续超出 00:0-1 等。我该如何解决这个问题?
import Foundation
import UIKit
import AVFoundation
class Finale : UIViewController {
@IBOutlet weak var timerLabel: UILabel!
var timer = NSTimer()
var count = 240
var timerRunning = false
override func viewDidLoad() {
super.viewDidLoad()
}
func updateTime() {
count--
let seconds = count % 60
let minutes = (count / 60) % 60
let hours = count / 3600
let strHours = hours > 9 ? String(hours) : "0" + String(hours)
let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
if hours > 0 {
timerLabel.text = "\(strHours):\(strMinutes):\(strSeconds)"
}
else {
timerLabel.text = "\(strMinutes):\(strSeconds)"
}
}
@IBAction func startTimer(sender: AnyObject) {
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
func stopTimer() {
if count == 0 {
timer.invalidate()
timerRunning = false
}
}
@IBAction func stopTimer(sender: AnyObject) {
timerRunning = false
if count == 0 {
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("stopTimer"), userInfo: nil, repeats: true)
timerRunning = true
}}
}
func updateTime() {
count--
let seconds = count % 60
let minutes = (count / 60) % 60
let hours = count / 3600
let strHours = hours > 9 ? String(hours) : "0" + String(hours)
let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
if hours > 0 {
timerLabel.text = "\(strHours):\(strMinutes):\(strSeconds)"
}
else {
timerLabel.text = "\(strMinutes):\(strSeconds)"
}
stopTimer()
}
请记住,您的计时器不会倒数到零 - 您可以在代码中实现它。计时器每秒触发一次。
在您的 updateTime 函数中,您需要使计时器无效,并在计时器用完时调用您的声音函数
耶!它的工作!我混合使用了这两个答案。我将 stopTimer() 添加到我的 updateTimer 函数中,我从计时器中删除了 "var",并删除了代码的最后一个 paragraph/IBAction。十分感谢大家!现在我将尝试添加声音。 :)
我是 Swift 的新手 - 正在尝试为 iPhone/iPad 构建应用程序。希望您能够帮助我。
我想添加一个从 04:00 分钟倒数到 00:00 的计时器。然后它应该停在零并触发声音效果(我还没有尝试实现)。当您按下开始按钮时,倒计时开始(在我的代码中,startTimer 和 stopTimer 指的是同一个按钮;但是,按钮在开始时只被按下一次)。
计时器开始倒计时。它按计划将秒数转换为分钟数。但是,我的主要问题是我无法让倒计时停在零。它继续超出 00:0-1 等。我该如何解决这个问题?
import Foundation
import UIKit
import AVFoundation
class Finale : UIViewController {
@IBOutlet weak var timerLabel: UILabel!
var timer = NSTimer()
var count = 240
var timerRunning = false
override func viewDidLoad() {
super.viewDidLoad()
}
func updateTime() {
count--
let seconds = count % 60
let minutes = (count / 60) % 60
let hours = count / 3600
let strHours = hours > 9 ? String(hours) : "0" + String(hours)
let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
if hours > 0 {
timerLabel.text = "\(strHours):\(strMinutes):\(strSeconds)"
}
else {
timerLabel.text = "\(strMinutes):\(strSeconds)"
}
}
@IBAction func startTimer(sender: AnyObject) {
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
func stopTimer() {
if count == 0 {
timer.invalidate()
timerRunning = false
}
}
@IBAction func stopTimer(sender: AnyObject) {
timerRunning = false
if count == 0 {
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("stopTimer"), userInfo: nil, repeats: true)
timerRunning = true
}}
}
func updateTime() {
count--
let seconds = count % 60
let minutes = (count / 60) % 60
let hours = count / 3600
let strHours = hours > 9 ? String(hours) : "0" + String(hours)
let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
if hours > 0 {
timerLabel.text = "\(strHours):\(strMinutes):\(strSeconds)"
}
else {
timerLabel.text = "\(strMinutes):\(strSeconds)"
}
stopTimer()
}
请记住,您的计时器不会倒数到零 - 您可以在代码中实现它。计时器每秒触发一次。
在您的 updateTime 函数中,您需要使计时器无效,并在计时器用完时调用您的声音函数
耶!它的工作!我混合使用了这两个答案。我将 stopTimer() 添加到我的 updateTimer 函数中,我从计时器中删除了 "var",并删除了代码的最后一个 paragraph/IBAction。十分感谢大家!现在我将尝试添加声音。 :)