Swift - 计时器倒计时游戏

时间:2014-12-19 10:58:18

标签: ios swift timer countdown

我已经在网上看过很多不同的'定时器'教程,但他们过于复杂的背景e.t.c.我想要的是屏幕顶部的计时器倒计时3,2,1,Play'。我会有图像而不是文字。一旦点击了触摸开始按钮(我已经做过),就会激活此计时器。在Apple Swift中你可以给我的任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:3)

您是否尝试过scheduledTimerWithTimeInterval

中的方法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()
    }
 }