使用.TimeIntervalsince()在Swift中创建秒表

发布于 2025-01-22 01:03:58 字数 214 浏览 1 评论 0原文

我需要使用Method TimeIntervalsince()在Swift中创建一个简单的秒表。 我真的不明白如何使用TimeIntervals(我需要的内容以及如何实施)以及如何将其转换为一个将显示的字符串,该字符串将向我显示“ 00:00:00”之类的经过的时间。

我知道我需要使用计时器来更新标签并在单击“停止”时将其无效。

我真的很感谢您的任何帮助。让我知道您是否需要更多信息。

I need to create a simple stopwatch in Swift using the method timeIntervalSince().
I don't really understand how to use timeIntervalSince (what I need and how to implement it) and how to transform it into a String that will show me the passed time like "00:00:00".

I know I need to use a Timer to update the Label and invalidate it when clicking on "Stop".

I'd really appreciate any help on this. Let me know if you need more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2025-01-29 01:03:58
//
//  StopWatchVC.swift
//  Gem
//
//  Created by Macbook 5 on 4/18/22.
//

import UIKit

class StopWatchVC:UIViewController {
    
    var timer:Timer?
    var startTime = Date()
    let titleLabel = UILabel()
   
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(titleLabel)
        titleLabel.frame = CGRect(x: 0, y: 0, width: 200, height: 60)
        titleLabel.center = view.center
        titleLabel.textColor = .red
        view.backgroundColor = .white
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: (#selector(updateTimer)), userInfo: nil, repeats: true)
    }
    
    @objc func updateTimer() {
        let timeInterval =  Date().timeIntervalSince(startTime)
        titleLabel.text = timeInterval.stringFromTimeInterval()
    }
}
extension TimeInterval{
    
    func stringFromTimeInterval() -> String {
        
        let time = NSInteger(self)
        
        let ms = Int((self.truncatingRemainder(dividingBy: 1)) * 1000)
        let seconds = time % 60
        let minutes = (time / 60) % 60
        let hours = (time / 3600)
        
        return String(format: "%0.2d:%0.2d:%0.2d.%0.3d",hours,minutes,seconds,ms)
        
    }
}
//
//  StopWatchVC.swift
//  Gem
//
//  Created by Macbook 5 on 4/18/22.
//

import UIKit

class StopWatchVC:UIViewController {
    
    var timer:Timer?
    var startTime = Date()
    let titleLabel = UILabel()
   
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(titleLabel)
        titleLabel.frame = CGRect(x: 0, y: 0, width: 200, height: 60)
        titleLabel.center = view.center
        titleLabel.textColor = .red
        view.backgroundColor = .white
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: (#selector(updateTimer)), userInfo: nil, repeats: true)
    }
    
    @objc func updateTimer() {
        let timeInterval =  Date().timeIntervalSince(startTime)
        titleLabel.text = timeInterval.stringFromTimeInterval()
    }
}
extension TimeInterval{
    
    func stringFromTimeInterval() -> String {
        
        let time = NSInteger(self)
        
        let ms = Int((self.truncatingRemainder(dividingBy: 1)) * 1000)
        let seconds = time % 60
        let minutes = (time / 60) % 60
        let hours = (time / 3600)
        
        return String(format: "%0.2d:%0.2d:%0.2d.%0.3d",hours,minutes,seconds,ms)
        
    }
}
情泪▽动烟 2025-01-29 01:03:58

方法TimeIntervals(_:)date的方法。它为您提供自其他日期和您询问的日期以来已通过的秒数。

因此,

创建一个stopwatchvc。
给StopWatchVC A starttime类型日期的var。
还要给它一个计时器 var。让我们称之为UpdateTimer

当用户点击开始按钮时,将date()(现在的时间)保存到开始时间。还要启动重复计时器,updateTimer每1/10秒发射一次。 (或者经常您想更新秒表,但是请注意,比1/60快的速度毫无意义,因为屏幕不能更快地更新,而计时器仅适用于约1/50秒。)

每次<)代码> UpdateTimer 触发,计算自开始时间以来经过的秒数,并将其显示到屏幕上:

let seconds = Date().timeIntervalSince(startTime)

date()是当前日期和时间,具有子毫秒精度。
date()。timeIntervals(starttime)将为您提供start Time的秒数,再次具有子毫秒精度。

格式化并将经过的时间显示到屏幕上。您可以使用dateComponentsFormatter或使用numberFormatter或什至string(格式:)自己构建时间字符串

The method timeIntervalSince(_:) is a method of Date. It gives you the number of seconds that have passes since some other date and the date you are asking.

So,

Create a StopwatchVC.
Give StopwatchVC a startTime var of type Date.
Also give it a Timer var. Lets call that updateTimer.

When the user taps the start button, save Date() (the time right now) into startTime. Also start a repeating timer, updateTimer that fires every 1/10 second. (or however often you want to update your stopwatch, but note that faster than 1/60 is pointless because the screen can't update any faster than that, and timers are only accurate to about 1/50 sec anyway.)

Each time updateTimer fires, calculate the number of seconds that have elapsed since the start time and display it to the screen:

let seconds = Date().timeIntervalSince(startTime)

Date() is the current date and time, with sub-millisecond accuracy.
Date().timeIntervalSince(startTime) will give you the number of seconds since startTime, again with sub-millisecond accuracy.

Format and display the elapsed time to the screen. You could use a DateComponentsFormatter or build a time string yourself using a NumberFormatter, or even String(format:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文