타이머처러 시간마다 처리하고 싶은 작업이 있는 경우 사용함. GCD API로 제공된다.
func startTimer(){
if self.timer == nil{
self.timer = DispatchSource.makeTimerSource(flags: [], queue:DispatchQueue.main)
self.timer?.schedule(deadline: .now(), repeating: 1.0)
self.timer?.setEventHandler(handler: { [weak self] in
guard let self = self else{return}
self.currentSeconds -= 1
let hour = self.currentSeconds/3600
let minutes = (self.currentSeconds%3600)/60
let seconds = (self.currentSeconds % 3600)%60
self.timerLabel.text = String(format: "%02d:%02d:%02d",hour,minutes,seconds)
self.progressView.progress = Float(self.currentSeconds)/Float(self.duration)
UIView.animate(withDuration: 0.5, delay: 0) {
self.imageView.transform = CGAffineTransform(rotationAngle: .pi)
}
UIView.animate(withDuration: 0.5, delay: 0.5) {
self.imageView.transform = CGAffineTransform(rotationAngle: .pi*2)
}
if self.currentSeconds ?? 0 <= 0 {
AudioServicesPlaySystemSound(1005)
self.stopTimer()
}
})
self.timer?.resume()
}
}
DispatchSource.makeTimerSource(flags: [], queue: )
queue→GCD 큐중 어디서 실행할 것인가
전체코드