You want to show the countdown timer or stopwatch or any task related timer. So you have to implement by yourself by default iOS not provide any functionality related to the timer.To implement a timer in your application you have one thing with (you number of days or hours or minutes or seconds).Suppose you have Seconds with you and then calculate hours,minutes and days from seconds and you have to implement timer class and his function scheduledTimer() to implement it and run until time gets over. Here’s the example stopwatch:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
var timer1 = Timer() self.timer1 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runScheduledTask), userInfo: nil, repeats: true) var secondsLeft: Int = 20000 func runScheduledTask(_ runningTimer: Timer) { var hour: Int var minute: Int var second: Int var day: Int secondsLeft -= 1 if secondsLeft == 0 || secondsLeft < 0{ timer1.invalidate() } else { hour = secondsLeft / 3600 minute = (secondsLeft % 3600) / 60 second = (secondsLeft % 3600) % 60 day = ( secondsLeft / 3600) / 24 if(day > 0){ hour = (secondsLeft / 3600) % (day * 24) } // days.text = String(format: "%02d", day) // minutes.text = String(format: "%02d", minute) // seconds.text = String(format: "%02d", second) // hours.text = String(format: "%02d", hour) } } |
Be the first to comment.