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()

댓글

이 블로그의 인기 게시물

한글 2010 에서 Ctrl + F10 누르면 특수문자 안뜰 때

아이폰에서 RFID 사용하는 방법

VCC 와 GND 는 무엇일까?