我需要多个不同时期的倒数计时器。如何在相同的活动和相同的标签中停止一个并启动另一个。
我现在有这个代码:
import UIKit
类ViewController:UIViewController {
@IBOutlet weak var countDownLabel: UILabel!
var count = Int()
var timer = Timer()
var timerIndex = Int()
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
var timerHasFinishedRunning: Bool = false
func update() {
let minutes = String(count / 60)
let seconds = String(count % 60)
if count == 0 {
if timerIndex == 0 { count = 12
}
else if timerIndex == 1 {
count = 11
}
else if timerIndex == 2 {
count = 10
}
else if timerIndex == 3 {
count = 9
}
else {
timer.invalidate()
countDownLabel.text = "The timer has finished running!"
timerHasFinishedRunning = true
}
if timerHasFinishedRunning == false{
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
timer.invalidate()
timerIndex += 1
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
}
else {
if timerIndex == 0{
count -= 1
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
}
else if timerIndex == 1{
count -= 1
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
}
else if timerIndex == 2{
count -= 1
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
}
else if timerIndex == 3{
count -= 1
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
如果您希望格式为M:S:
,请将update()
功能替换为此功能
var timerHasFinishedRunning: Bool = false
func update() {
if count == 0{
if timerIndex == 0{ count = 200 // or the desired value for the second timer
}
else if timerIndex == 1{
count = 40 // or the desired value for the third timer
}
else {
timer.invalidate()
countDownLabel.text = "The timer has finished running!"
timerHasFinishedRunning = true
}
if timerHasFinishedRunning == false{
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
timer.invalidate()
timerIndex += 1
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
}
else {
if timerIndex == 0{
count -= 1
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
}
else if timerIndex == 1{
count -= 1
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
}
else if timerIndex == 2{
count -= 1
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
}
}
}
将代码替换为此代码,然后添加新值!格式:MM:SS:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var countDownLabel: UILabel!
var count = Int()
var timer = Timer()
var timerIndex = 0
var timerHasFinishedRunning: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
count = 5
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
func update() {
if count == 0{
if timerIndex == 0{ count = 3 // or the desired value for the second timer
}
else if timerIndex == 1{
count = 2 // or the desired value for the third timer
}
else {
timer.invalidate()
countDownLabel.text = "The timer has finished running!"
timerHasFinishedRunning = true
}
if timerHasFinishedRunning == false{
let minutes = Int(count / 60)
let seconds = Int(count % 60)
if minutes > 9{
if seconds >= 10{
countDownLabel.text = "\(minutes)" + ":" + "\(seconds)"
}
else if seconds < 10{
countDownLabel.text = "\(minutes)" + ":" + "0\(seconds)"
}
}
else if minutes<=9{
if seconds >= 10{
countDownLabel.text = "0\(minutes)" + ":" + "\(seconds)"
}
else if seconds < 10{
countDownLabel.text = "0\(minutes)" + ":" + "0\(seconds)"
}
}
timer.invalidate()
timerIndex += 1
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
}
else {
count -= 1
let minutes = Int(count / 60)
let seconds = Int(count % 60)
if minutes > 9{
if seconds >= 10{
countDownLabel.text = "\(minutes)" + ":" + "\(seconds)"
}
else if seconds < 10{
countDownLabel.text = "\(minutes)" + ":" + "0\(seconds)"
}
}
else if minutes<=9{
if seconds >= 10{
countDownLabel.text = "0\(minutes)" + ":" + "\(seconds)"
}
else if seconds < 10{
countDownLabel.text = "0\(minutes)" + ":" + "0\(seconds)"
}
}
}
}
}
如果我想找到解决问题的方法,我会再次更新答案。 希望这有帮助!
答案 1 :(得分:0)
试试这个......只是根据你的要求改变
var count = Int()
var timer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a
printSecondsToHoursMinutesSeconds(100)
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
func update() {
count = 1 + count
if(count != 0) {
printSecondsToHoursMinutesSeconds(count)
} else {
timer.invalidate()
}
}
func printSecondsToHoursMinutesSeconds (seconds:Int) -> () {
let (h, m, s) = secondsToHoursMinutesSeconds (Double(seconds))
print ("\(h) Hours, \(m) Minutes, \(s) Seconds")
}
func secondsToHoursMinutesSeconds (seconds : Double) -> (Double, Double, Double) {
let (hr, minf) = modf (seconds / 3600)
let (min, secf) = modf (60 * minf)
return (hr, min, 60 * secf)
}