Please tell me how to increase the number by one every 0.25 seconds.

Asked 2 years ago, Updated 2 years ago, 86 views

I'd like to write a code that says the number increases with time.
As shown in the code below, we can increase the number every second, but
You cannot increase a decimal number, such as every 0.25 seconds.
I tried various methods, but it didn't work well, so I got stuck.

 iflet=self.startTime{
        let time —Double=NSDate.timeIntervalSinceReferenceDate()-t+self.elappedTime
        letsec: Int=Int(time)
        let msec: Int=Int(time-Double(sec))*100.0)
        self.timerLabel.text=NSString(format: "%02d:%02d:%02d:%02d", sec/60, sec%60, msec) as String
        self.resultLabel.text=NSString(format: "%01d", sec*4) as String
    }


of the above code self.resultLabel.text=NSString(format: "%01d", sec*4)asString

The part is the label that increases the number.
Currently, the number increases by 4 in 1 second, but
I would like to set this to increase the number by 1 in 0.25 seconds.
If you do not have enough information, I will add it, so please let me know.

Thank you for your cooperation.

ios swift xcode iphone

2022-09-30 18:36

1 Answers

letsec: Int=Int(time) has reduced the granularity of the data, so even if you multiply it by four times, it will only increase by four per second.

 iflet=self.startTime{
    let time —Double=NSDate.timeIntervalSinceReferenceDate()-t+self.elappedTime
    letsec: Int=Int(time)
    letsec4: Int=Int(time*4)
    let msec: Int=Int(time-Double(sec))*100.0)
    self.timerLabel.text=NSString(format: "%02d:%02d:%02d:%02d", sec/60, sec%60, msec) as String
    self.resultLabel.text=NSString(format: "%01d", sec4) as String
  }

I think it would be good to say that


2022-09-30 18:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.