2014-12-19 2 views
0

나는 많은 다른 '타이머'튜토리얼을 온라인에서 보았지만, 배경 e.t.c와 같은 것들로 지나치게 복잡하다. 내가 원하는 것은 '3, 2, 1, Play'를 카운트 다운하는 화면 상단의 타이머입니다. 텍스트가 아닌 이미지가 있습니다. 이 타이머는 터치 - 투 - 시작 버튼 (내가 이미 만들었습니다)이 클릭하자마자 활성화됩니다. Apple Swift에서이 작업을 수행 할 수있는 도움이 있으면 대단히 감사하겠습니다.스위프트 - 게임 플레이를위한 타이머 카운트 다운

+0

에서 방법 scheduledTimerWithTimeInterval을 시도나요, 당신은 몇 가지 아이디어를 얻을 수 있습니다. – Suresh

답변

3

는 MZTimerLabel 라이브러리 https://github.com/mineschan/MZTimerLabel 주변에 플레이 NSTimer

// IBOutlet From Storyboard 
@IBOutlet weak var imgView: UIImageView! 

var countdown : Int! = 0 // Variable to check the count down seconds 

override func viewDidLoad() { 
     // Scheduling timer to Call the function **Countdown** with the interval of 1 seconds 
     NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDown:"), userInfo: nil, repeats: true) 
} 

func countDown(timer: NSTimer) { 
    countdown = countdown + 1 
    if (countdown == 1) {  // second 1, show image 3 
     imgView.image = UIImage(named: "three.png") 
    } 
    else if (countdown == 2) { // second 2, show image 2 
     imgView.image = UIImage(named: "two.png") 
    } 
    else if (countdown == 3) { // second 3, show image 1 
     imgView.image = UIImage(named: "one.png") 
    } 
    else if (countdown == 4) { // second 4, show image 'play' 
     imgView.image = UIImage(named: "play.png")    
    } 
    else { 
    // Invalidate the timer & remove the `time image` and continue with your `gameplay` 
     imgView.removeFromSuperview() 
     timer.invalidate() 
    } 
} 
+0

코드의 다른 섹션을 설명하기 위해 주석을 사용하십시오. 고맙습니다. –

+0

덧글 추가, 죄송합니다. 나는'code'에서 실수를 저질렀습니다. 나는 그것을 편집했습니다. – arthankamal

+0

viewDidLoad를 사용하는 대신 내 시작 버튼이 숨겨져있을 때 연결할 수 있습니까? @IBAction func touchToBegin (발신자 : AnyObject) { btnBegin.hidden = true } –