2016-08-28 8 views
0

주어진 루프 수에 대해서만 루프 내에서 타이머를 실행하고 싶습니다. 외부 루프도 있습니다.Swift에서 for 루프의 타이머

사용자는 원하는 반복 횟수, 반복 횟수, 두 카운트 간의 시간 간격을 입력합니다.

앱에 각 반복 횟수가 표시됩니다. 모든 반복이 완료되면 앱이 타이머를 중지합니다.

하지만 어려운 것으로 알고 있습니다. timer는 for 루프를 무시하고 timer.invalidate() 만 실행할 때 멈추는 루프입니다.

의견이 있으십니까?

for x in 0...HowManyRepetitions { 

       counter = 0 
       CountLabel.text = "\(counter)" 
       RepetitionLabel.text = "\(x)/\(HowManyRepetitions)" 

       for y in 0...HowManyCounts { 

        timer = NSTimer.scheduledTimerWithTimeInterval(PeriodBetween, target: self, selector: updateCounter, userInfo: nil, repeats: true) 

       } 

      } 
+0

귀하의 타이머가 실제로 아무것도하지 않습니다

var timers: [NSTimer] = [] func startTimers(howManyRepetitions: Int, howManyCounts: Int, periodBetween: NSTimeInterval) { timers = [] for x in 1...howManyRepetitions { let userInfo: NSMutableDictionary = [ "counter": 0, "howManyCounts": howManyCounts, "myName": "Timer-\(x)" ] timers.append(NSTimer.scheduledTimerWithTimeInterval(periodBetween, target: self, selector: #selector(timerHandler), userInfo: userInfo, repeats: true)) } } 

는 타이머를 시작합니다. 왜 타이머가 있니? – Paulw11

답변

1

반복 횟수는 타이머 처리기에서 관리해야합니다.

일반적으로 타이머를 인스턴스 속성으로 유지합니다. (당신은 때 viewWillDisappear(_:)을 예를 들어, 타이머를 무효화해야 할 수도 있습니다.) 그래서

, 당신이 당신의 클래스에 다음처럼 작성할 필요가 있습니다 :

var timer: NSTimer? 
func startTimer(howManyCounts: Int, periodBetween: NSTimeInterval) { 
    let userInfo: NSMutableDictionary = [ 
     "counter": 0, 
     "howManyCounts": howManyCounts, 
     "myName": "Timer" 
    ] 
    self.timer = NSTimer.scheduledTimerWithTimeInterval(periodBetween, target: self, selector: #selector(timerHandler), userInfo: userInfo, repeats: true) 
} 

@objc func timerHandler(timer: NSTimer) { 
    guard let info = timer.userInfo as? NSMutableDictionary else { 
     return 
    } 
    var counter = info["counter"] as? Int ?? 0 
    let howManyCounts = info["howManyCounts"] as? Int ?? 0 
    let myName = info["myName"] as? String ?? "Timer" 
    counter += 1 

    print("\(myName):\(counter)") //countLabel.text = "\(counter)" 

    if counter >= howManyCounts { 
     timer.invalidate() 
    } else { 
     info["counter"] = counter 
    } 
} 

이 방법에 어딘가에서 타이머를 시작합니다 같은 클래스는 다음과 같이 : 당신이 외부 루프가 필요한 이유

startTimer(10, periodBetween: 3.0) 

이해가되지 않지만, 여러 타이머가 작동하게하려면, 당신은 모든 타이머를 유지해야합니다. 그것이 전무 선택이 있기 때문에

startTimers(3, howManyCounts: 4, periodBetween: 1.0) 
1

여러분의 타이머가 루프를 초과해야한다고 생각합니다. 예를 들어

:

for x in 0...HowManyRepetitions { 

       counter = 0 
       CountLabel.text = "\(counter)" 
       RepetitionLabel.text = "\(x)/\(HowManyRepetitions)" 

       timer = NSTimer.scheduledTimerWithTimeInterval(PeriodBetween, target: self, selector: nil, userInfo: nil, repeats: true) 

       for y in 0...HowManyCounts { 
        // doSomething 
        ... 
       } 

       timer.invalidate() 

      } 
0

정확하게 당신이 요구하는지 모르고, 당신은 루프에서 설정할 수 있습니다. 자세한 내용은 분명 도움이 될 것입니다.

let param = 0 //IN SCOPE 

for y in 0...HowManyCounts { 

    param++ 

    if param != HowManyCounts{ 
    timer = NSTimer.scheduledTimerWithTimeInterval(PeriodBetween, target: self, selector: nil, userInfo: nil, repeats: true) 
    }else{ 
     timer.invalidate() 
    } 

}