Me está costando lograr algo. Estoy usando Firebase
como base de datos. Cuando un usuario crea una nueva publicación de encuesta, también debe establecer una fecha de vencimiento. En la propia aplicación, tengo que mostrar la fecha de vencimiento como un temporizador (¿o una cuenta regresiva?) En un UILabel
, que muestra cuánto tiempo queda antes de que la votación deba desactivarse.
Esto es lo que tengo hasta ahora:
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
}
}
Lo único que hace el código anterior es simplemente obtener la fecha y configurarla pollExpirationDate.text
básicamente a la hora que sea. El formato de salida es así:
3d 3h 34min 5s
Qué tengo que hacer y no sé cómo (necesito ayuda con el código):
Tengo que asegurarme de que el tiempo no sea negativo. En este momento, simplemente muestra la hora, incluso si es negativa. Si el tiempo es negativo, significa que pollExpirationDate UILabel
simplemente debería decir que la encuesta ha terminado, deshabilite las opciones de votación (que son UIButton
) y muestre un bloque con el resultado (ahora, no espero esto de usted, esto ni siquiera es establecido todavía en este punto, solo necesito saber dónde debo ejecutar este código, eso es todo)
Estoy descargando la fecha FirebaseDatabase
y configurando el UILabel
, pero no sé cómo crear el temporizador (o cuenta regresiva) que debería animar el pollExpirationDate
UILabel
(tiempo restante)
Cuando la cuenta atrás llega a 0 segundos, la votación debe desactivarse (el tiempo ha expirado)
Nunca he hecho nada de esto, por lo que la ayuda es muy apreciada.
Cree dos variables globalmente en clase para verificar que el estado del temporizador esté funcionando o en pausa / detenido.
fileprivate var timeWorking : Bool = false
var timer : Timer?
Luego, creó un método para obtener todos los componentes de fecha como los que describió.
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
}
Esto me dará la fecha en 3d 3h 34min 5s
formato.
Para animar el contador de make Timer y llamar a este método.
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"
}
}
}
El método de contador de actualización contará hacia atrás el temporizador de fecha.
@objc func updateCountDown() {
if let expirationTimeInt = post?.pollExpirationTime {
let expirationDate = Date(timeIntervalSince1970: Double(expirationTimeInt))
self.pollExpirationDate.attributedText = self.timeLeftExtended(date: expirationDate)
}
}
Llame al método del temporizador de configuración desde viewDidLoad. Este es un código de trabajo para mí, espero que esto te ayude.
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras