| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- //
- // CountDownManager.swift
- // QuickSearchLocation
- //
- // Created by Destiny on 2025/9/24.
- //
- import UIKit
- class QSLCountdownManager {
- static let shared = QSLCountdownManager()
- private var timer: Timer?
- var selectGood: QSLGoodModel?{
- didSet{
- if(oldValue != nil){
- NotificationCenter.default.post(
- name: Notification.Name("QSLHomeRefreshCouponViewNoti"),
- object: nil,
- userInfo: nil
- )
- }
- }
- }
- var remainingSeconds: Int = 900 * 1000 // 15分钟 = 900秒
- var updateHandler: ((String) -> Void)?
- var updateHandler1: ((String) -> Void)?
-
- var finishHandler: (() -> Void)?
-
- func startCountdown() {
- timer?.invalidate()
- timer = nil
- if(selectGood == nil){
- return
- }
- remainingSeconds = 900 * 1000
- timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)
- RunLoop.current.add(timer!, forMode: .common)
- }
-
- func checkIsCountDown() -> Bool{
- if let curTimer = timer {
- return true
- }else{
- return false
- }
- }
-
- func stopCountdown() {
- finishHandler?()
- timer?.invalidate()
- timer = nil
- }
-
- @objc private func updateCountdown() {
- remainingSeconds -= 1
- if remainingSeconds <= 0 {
- stopCountdown()
- }
-
- let totalSeconds = remainingSeconds / 1000
- let minutes = totalSeconds / 60
- let seconds = totalSeconds % 60
- let milliseconds = remainingSeconds % 1000
-
- // 格式:MM:SS:SSS(例如 "14:59:500")
- let timeString = String(format: "%02d : %02d : %03d", minutes, seconds, milliseconds)
- updateHandler?(timeString)
- updateHandler1?(timeString)
- }
-
- deinit {
- stopCountdown()
- }
- }
|