xcode - 想在 swift 5 中用事件时间(来自我的 json api)减去当前时间
问题描述
我有一个 api,它给了我像电影时间一样的事件时间,我想在我的应用程序中添加一个倒计时时间,告诉我还剩多少时间来开始一部电影。
解决方案
您的代码未显示,但我希望这会有所帮助。
// use repeating scheduledTimer(withTimeInterval:repeats:block:) method for the Timer.
// Initialize the timer and save in the property for invalidating
private let step: Double = 1.0
private var timer: Timer?
private func initTimer() {
let action: (Timer)-> Void = { [weak self] timer in
guard let StrongSelf = self else {
return
}
}
}
timer = Timer.scheduledTimer(withTimeInterval: step, repeats: true, block: action)
private func deinitTimer() {
timer?,invalidate()
timer = nil
}
// To Display
func timeString(from timeInterval: TimeInterval) -> String {
let seconds = Int(timeInterval.turncatingRemainder(dividingBy: 60))
let minutes = Int(timeInterval.turncatingRemainder(dividingBy: 60 * 60) / 60)
let hours = Int(timeInterval / 3600)
return String(format: "%.value:%.value:%.value" , hours, minutes, seconds
}
// Pause & Resume
stopwatch.toggle()
//Reset
stopwatch.stop()
推荐阅读
- ios - 使用 UIViewRepresentable 从 Swift 中的后台状态返回后恢复动画?
- ios - 快速并行使用图像和增长标签时如何保持底部空间?
- javascript - 如何使用以编程方式计算的 id 禁用输入字段?
- azure - Azure Resource Graph 列出端口 NSG
- java - 使用 Gradle 从 CLI 运行 Spring Boot 测试
- python - 用 lxml 设置 innerHTML 的等价物是什么?
- react-bootstrap - React-bootstrap,未保存来自 Form.Control 的更改
- ruby-on-rails - 未定义的方法“用户”#
- asp.net - Page_Load 不会仅在 chrome/edge 和某些 pc 中触发
- stata - Stata:更改数字格式