Таким образом, вы можете запустить таймер 30 раз за 1 секунду.После 30-кратного запуска он автоматически остановится
// Timer That will be monitored
var myTimerToRepeat:Timer?
/// Number of times Timer ran
private var myTimerRunCount:Int?
/// Number of times Timer need to Run
private var numOfRepeatition:Int?
/// Time Interval for how long timer need to run
var myTimeInterval : TimeInterval = 1
/// Timer Handler
@objc func repeatTimerValue() {
myTimerRunCount!+=1
print("Repeat Count \(myTimerRunCount!)")
/// Check is Timer Ran Max ?
if myTimerRunCount! == self.numOfRepeatition! {
/// yes Stop
myTimerToRepeat!.invalidate()
myTimerToRepeat=nil
}
}
/// Start Timer
func startTimer(WithInterval Inter: TimeInterval) {
if myTimerToRepeat != nil {
myTimerToRepeat!.invalidate()
myTimerToRepeat=nil
}
/// Start Timer
myTimerToRepeat = Timer.scheduledTimer(timeInterval: Inter, target: self, selector: #selector(repeatTimerValue), userInfo: nil, repeats: true)
}
override func viewDidLoad() {
super.viewDidLoad()
/// Set Count to 0
myTimerRunCount=0
/// Set the num of Repeatitions
numOfRepeatition=30
/// Start Timer
startTimer(WithInterval: myTimeInterval/Double(numOfRepeatition!))
}