11월, 2018의 게시물 표시

swift 용 timer 만들기

순수 스위프트용 타이머 만들기 먼저 타이머 선언을 한다 let total = 0 let timer = DispatchSource.makeTimerSource(queue: DispatchQueue(label: "swiftTimer", attributes: .concurrent)) 이제 타이머를 ViewDidLoad 같은 곳에 설정한다. timer.schedule(deadline: .now(), repeating: .milliseconds(100)) timer.setEventHandler {   self.total += 1   if self.total >= 30 { self.timer.cancel() }   DispatchQueue.main.async {     self.label.text = "\(self.total)"   } } 이제 타이머를 실행 시킬 곳에서 아래와 같이 해야 한다. self.timer.resume()