gpt4 book ai didi

ios - 如何为倒数计时器实现单独的分钟和秒标签

转载 作者:行者123 更新时间:2023-11-28 05:51:16 25 4
gpt4 key购买 nike

好的,所以我正在开发一个倒数计时器功能,作为我正在开发的应用程序的一部分。我在如何实现计时器及其在屏幕上的显示方式方面遇到了一些问题。我的两个问题是,

  1. 现在我只有计时器可以倒计时。计时器将始终有 10 分钟的固定倒计时时间。
  2. 我有两个单独的计时器标签,因为我希望它们以不同的方式显示在屏幕上。是否可以将分钟和秒变量推送到显示屏上的单独标签?

如果有什么我没说清楚的,尽管问。

enter image description here

    import UIKit

class ViewController: UIViewController {

//MARK: - Countdown Timer Connections
@IBOutlet weak var minuteLabel: UILabel!
@IBOutlet weak var secondLabel: UILabel!

//MARK: - Seed Connections
@IBOutlet weak var seedSlot1: UIImageView!
@IBOutlet weak var seedSlot2: UIImageView!
@IBOutlet weak var seedSlot3: UIImageView!
@IBOutlet weak var seedSlot4: UIImageView!

//MARK: - Start/Pause Button Connection
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var stopButton: UIButton!
@IBOutlet weak var pauseButton: UIButton!

//MARK: - Variables
var timer = Timer()

var isTimerRunning : Bool = false
var resumeTapped: Bool = false
var minutes : Int = 24
var seconds: Int = 10

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pauseButton.setImage(nil, for: .normal)
stopButton.setImage(nil, for: .normal)
}



//MARK: Timer Control Functions
//MARK: - Start Timer
@IBAction func startTimer(_ sender: UIButton) {
if isTimerRunning == false {
isTimerRunning = true
setButtons()

timer = Timer.scheduledTimer(
timeInterval: 1,
target: self,
selector: (#selector(ViewController.updateTimer)),
userInfo: nil,
repeats: true
)

print("The timer is currently running: \(isTimerRunning)")
} else {
isTimerRunning = false
setButtons()
print("The timer is currently running: \(isTimerRunning)")
}
print(secondLabel.text)
}

//MARK: - Stop Timer
@IBAction func stopTimer(_ sender: UIButton) {
timer.invalidate()
isTimerRunning = false
seconds = 60
minuteLabel.text = "\(minutes)"
secondLabel.text = "\(seconds)"
setButtons()
}

//MARK: - Pause Timer
@IBAction func pauseTimer(_ sender: UIButton) {
if resumeTapped == false {
timer.invalidate()
isTimerRunning = false
setButtons()
} else {
startTimer(startButton)
isTimerRunning = true
setButtons()
}
}

func timeString (time:TimeInterval) -> String {
let secondValue = Int(time) & 60
let minuteValue = Int(time) / 60 % 60
return String(format: "%02i", minuteValue, secondValue)
}

//MARK: - Update Timer
@objc func updateTimer() {
seconds -= 1 //This will decrement(count down)the seconds.
secondLabel.text = timeString(time: TimeInterval(seconds)) //This will update the seconds text label
}

最佳答案

如果您想通过递减seconds 变量的值来手动计算秒数,那可不是个好主意。因为 Timer 不保证它会在 1 秒间隔后触发。在我看来,如果您要计算计时器启动时间和当前时间之间的时间间隔,则首选方法。

class ViewController: UIViewController {

@IBOutlet weak var minuteLabel: UILabel!
@IBOutlet weak var secondLabel: UILabel!


var timer: Timer?

let countDownInterval: TimeInterval = 10*60 // minutes*seconds
var finishTime: Date!
var pauseStartTime: Date?

@IBAction func startTimer(_ sender: UIButton) {
if let pause = pauseStartTime {
let pauseInterval = Date().timeIntervalSince(pause)
finishTime.addTimeInterval(pauseInterval)
} else {
finishTime = Date().addingTimeInterval(countDownInterval)
}

pauseStartTime = nil
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { [weak self] _ in self?.changeLabels() })
}

@IBAction func pauseTimer(_ sender: UIButton) {
pauseStartTime = Date()
timer?.invalidate()
}

func changeLabels() {
let currentTime = Date()

guard currentTime < finishTime else {
// Countdown finished
timer?.invalidate()
minuteLabel.text = String(format: "%02.f", 0)
secondLabel.text = String(format: "%02.f", 0)
return
}

let currentInterval = finishTime.timeIntervalSince(currentTime)
let minutes = (currentInterval / 60).rounded(.down)
let seconds = currentInterval.truncatingRemainder(dividingBy: 60).rounded()
minuteLabel.text = String(format: "%02.f", minutes)
secondLabel.text = String(format: "%02.f", seconds)
}
}

关于ios - 如何为倒数计时器实现单独的分钟和秒标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52883661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com