gpt4 book ai didi

swift - 在标签中设置和动画化日期时间

转载 作者:行者123 更新时间:2023-11-28 14:31:19 25 4
gpt4 key购买 nike

我很难完成某件事。我正在使用 Firebase 作为数据库。当用户创建新的投票帖子时,他们还必须设置到期日期。在应用程序本身中,我必须在 UILabel 中将到期日期显示为计时器(或倒计时?),显示在投票应该被禁用之前剩余的时间。

这是我目前所拥有的:

extension Date {


func offsetFromNow() -> String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute, .second]
formatter.unitsStyle = .abbreviated
return formatter.string(from: Date(), to: self)!
}
}

class PostPollCVCell: UICollectionViewCell {
var seconds = 60
var timer = Timer()
var isTimerRunning = false
func updateView() {
if let expirationTimeInt = post?.pollExpirationTime {
let expirationDate = Date(timeIntervalSince1970: Double(expirationTimeInt))
self.pollExpirationDate.text = "poll ends in: " + expirationDate.offsetFromNow()
}
}

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

@objc func updateTimer() {
seconds -= 1 //This will decrement(count down)the days, hours, min and seconds.
// pollExpirationDate.text = Update the label with the total time left
}
}

上面代码所做的唯一一件事就是简单地获取日期并将 pollExpirationDate.text 设置为基本上任何时间。输出格式是这样的:

3d 3h 34min 5s

我必须做什么但不知道怎么做(需要代码方面的帮助):

  1. 我必须确保时间不是负数。现在,它只是显示时间,即使它是负数。如果时间为负,则意味着 pollExpirationDate UILabel 应该简单地说投票已经结束,禁用投票选项(即 UIButton)并显示一个包含结果的 block (现在,我不期望你会这样,此时甚至还没有设置,我只需要知道我应该在哪里运行这段代码,仅此而已)

    <
  2. 我正在从 FirebaseDatabase 下载日期并设置 UILabel,但我不知道如何创建计时器(或倒计时)为 pollExpirationDate UILabel(剩余时间)制作动画

  3. 当倒计时到0秒时,应该关闭投票(时间已过)

我从来没有做过这些,所以非常感谢帮助。

最佳答案

在类中全局创建两个变量来检查定时器的状态是工作还是暂停/停止。

fileprivate var timeWorking                     : Bool = false
var timer : Timer?

然后,创建了一种方法来获取与您所描述的相同的每个日期组件。

func timeLeftExtended(date:Date) -> NSAttributedString {

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

let fullCountDownStr = ""

if(components.day!.description == "0" || components.day!.description == "00") {

// This will display hour, minute, and second
fullCountDownStr = "\(components.hour!.description)h " + "\(components.minute!.description)m " + "\(components.second!.description)s "

} else if (components.day!.description == "0" || components.day!.description == "00") && (components.hour!.description == "0" || components.hour!.description == "00") {

// This will display minute and second
fullCountDownStr = "\(components.minute!.description)m " + "\(components.second!.description)s "

} else if (components.day!.description == "0" || components.day!.description == "00") && (components.hour!.description == "0" || components.hour!.description == "00") && (components.minute!.description == "0" || components.minute!.description == "00") {

// This will display second only
fullCountDownStr = "\(components.second!.description)s "

} else {

// This will display day, hour, minute, second
fullCountDownStr = "\(components.day!.description)d " + "\(components.hour!.description)h " + "\(components.minute!.description)m " + "\(components.second!.description)s "

}

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

for (index,char) in mutableStr.string.enumerated() {
if(char == "d" || char == "h" || char == "m" || char == "s") {
mutableStr.removeAttribute(NSAttributedStringKey.foregroundColor, range: NSMakeRange(index, 1))
mutableStr.addAttributes([.foregroundColor : UIColor.white], range: NSMakeRange(index, 1))
mutableStr.addAttributes([.font: UIFont.systemFont(ofSize: 12)], range: NSMakeRange(index, 1))
}
}

return mutableStr
}

这将给我 3d 3h 34min 5s 格式的日期。

为 make Timer 的计数器设置动画并调用此方法。

func setupTimer() {

if let expirationTimeInt = post?.pollExpirationTime {
let expirationDate = Date(timeIntervalSince1970: Double(expirationTimeInt))

if expirationDate == Date() {
self.pollExpirationDate.text = "expirationDate is Today's date"
} else if date! > Date() {
if(!timeWorking) {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateCountDown), userInfo: nil, repeats: true)
self.timeWorking = true
}
} else if date! < Date() {
self.pollExpirationDate.text = "expirationDate is gone"
}
}
}

更新计数器方法将为您的日期计时器倒计时。

@objc func updateCountDown() {
if let expirationTimeInt = post?.pollExpirationTime {
let expirationDate = Date(timeIntervalSince1970: Double(expirationTimeInt))

self.pollExpirationDate.attributedText = self.timeLeftExtended(date: expirationDate)
}
}

从 viewDidLoad 调用设置计时器方法。这是适合我的工作代码,希望对您有所帮助。

关于swift - 在标签中设置和动画化日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51229337/

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