【问题标题】:How to display 24 hour counter once button is pressed按下按钮后如何显示 24 小时计数器
【发布时间】:2019-09-11 06:32:59
【问题描述】:

一旦我的按钮被按下,我想禁用我的按钮 24 小时,并在标签上显示倒计时显示倒计时,直到按钮再次处于活动状态。

我已保存等待日期并将其与当前日期进行比较,但我不确定如何以小时、分钟和秒为单位显示剩余时间的倒计时。

let todaysDate = Date()

func set24HrTimer() {
    let currentDate = Date()
    let newDate = Date(timeInterval: 86400, since: currentDate as Date)

    UserDefaults.standard.set(newDate, forKey: "waitingDate")
    print("24 hours started")

    //disable the button

}


if let waitingDate:Date = UserDefaults.standard.value(forKey: "waitingDate") as? Date {
        if (todaysDate.compare(waitingDate as Date) == ComparisonResult.orderedDescending) {
            print("show button")

        }
        else {
            print("hide button")

        }
    }

【问题讨论】:

  • 使用 Date 而不是旧的 NSDate,Date 是正确的 swift 类。
  • 不要将value(forKey:)setValue(_:forKey:)UserDefaults 一起使用。使用正确的 API。如前所述,不要使用NSDate,使用Date
  • 有几种方法可以做到这一点,一种是使用DateComponentsFormatter

标签: ios swift


【解决方案1】:

您可以添加以下代码来创建时间倒计时。

首先添加两个变量ass如下:

fileprivate var timeWorking : Bool = false  // To check is timer already scheduled
var timer:Timer?  // Instance of timer

然后添加以下代码来计算剩余的小时、分钟和秒。

func timeLeftExtended(date:Date) ->NSAttributedString{

    let cal = Calendar.current
    let now = Date()
    let calendarUnits:NSCalendar.Unit = [NSCalendar.Unit.hour, NSCalendar.Unit.minute, NSCalendar.Unit.second]
    let components = (cal as NSCalendar).components(calendarUnits, from: now, to: date, options: [])

    let fullCountDownStr = "\(components.hour!.description)h " + "\(components.minute!.description)m " + "\(components.second!.description)s "

    let mutableStr = NSMutableAttributedString(string: fullCountDownStr, attributes: [NSAttributedString.Key.foregroundColor:UIColor.white])

    for (index, char) in mutableStr.string.enumerated()
    {
        if(char == "h" || char == "m" || char == "s")
        {
            mutableStr.removeAttribute(NSAttributedString.Key.foregroundColor, range: NSMakeRange(index, 1))
            mutableStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.lightGray], range: NSMakeRange(index, 1))
        }
    }

    return mutableStr
}

接下来,如果不是scheduled,则添加scheduledTimer 的代码。

func setupTimer()
{
    if(!timeWorking)
    {
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateCountDown), userInfo: nil, repeats: true)
        self.timeWorking = true
    }
}

添加代码以在标签中显示倒计时。

@objc func updateCountDown()
{
    if let waitingDate = UserDefaults.standard.value(forKey: "waitingDate") as? Date {

        self.labelCountDown.attributedText = self.timeLeftExtended(date: waitingDate)

    } else {
        let newDate = Calendar.current.date(byAdding: .hour, value: 24, to: Date())
        UserDefaults.standard.set(newDate, forKey: "waitingDate")
        self.labelCountDown.attributedText = self.timeLeftExtended(date: newDate!)
    }
}

调用setupTimer()方法继续定时器。

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多