一回以上RetryButtonを押し、ゲーム画面(GameViewControllerScene)を再表示すると、
all Dismiss Buttonを押してスタート画面(ViewControllerScene)を表示した際に、
invalidate()したタイマーが作動してしまいます。
タイマーが動いてしまわないようにする為にはどのようにしたら良いでしょうか。
環境
Xcode 7.1.1
使用ライブラリ
Spring(【よーい】【どん】のアニメーション機能に)
Swift
1// ViewController.swift 2 3import UIKit 4 5class ViewController: UIViewController { 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 } 11 12 override func didReceiveMemoryWarning() { 13 super.didReceiveMemoryWarning() 14 } 15 16}
Swift
1// GameViewController.swift 2 3import UIKit 4 5class GameViewController: UIViewController { 6 7 @IBOutlet weak var readeyGoLabel: DesignableLabel! 8 @IBOutlet weak var timerLabel: UILabel! 9 10 var cnt: Float = 20.0 11 var timer: NSTimer! 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 timerLabel.text = "TIME: \(self.cnt)" 17 readeyGoLabel.text = "" 18 19 } 20 21 override func viewDidAppear(animated: Bool) { 22 readyGoAnimation() 23 } 24 25 func readyGoAnimation() { 26 27 readeyGoLabel.text = "よーい" 28 readeyGoLabel.animation = "fadeIn" 29 readeyGoLabel.curve = "spring" 30 readeyGoLabel.duration = 1.4 31 readeyGoLabel.animateNext { () -> () in 32 33 self.readeyGoLabel.text = "どん" 34 self.readeyGoLabel.animation = "fadeOut" 35 self.readeyGoLabel.curve = "spring" 36 self.readeyGoLabel.duration = 0.7 37 self.readeyGoLabel.delay = 0.1 38 self.readeyGoLabel.animate() 39 40 self.timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "countDown:", userInfo: nil, repeats: true) 41 } 42 } 43 44 func countDown(timer : NSTimer) { 45 46 if cnt < 0 { 47 timer.invalidate() 48 self.timerLabel.text = "TIME: 0.0" 49 50 } else { 51 52 let str = "TIME: ".stringByAppendingFormat("%.1f",cnt) 53 print(str) 54 timerLabel.text = str 55 cnt -= 0.1 56 } 57 58 } 59 60 override func didReceiveMemoryWarning() { 61 super.didReceiveMemoryWarning() 62 } 63 64 @IBAction func retryButtonTapped(sender: AnyObject) { 65 timer.invalidate() 66 } 67 68 @IBAction func allDismissButtonTapped(sender: AnyObject) { 69 timer.invalidate() 70 UIApplication.sharedApplication().keyWindow!.rootViewController?.dismissViewControllerAnimated(true, completion: nil) 71 } 72 73} 74
補足
アプリには、主に下記のような機能を実装しています。
- スタート画面(ViewControllerScene)からゲーム画面(GameViewControllerScene)に遷移する。
- ゲーム画面に遷移するとfadeInで【よーい】、fadeOutで【どん】と表示される。
- NSTimer.scheduledTimerWithTimeIntervalを利用して、20秒のカウントダウンが始まる。
- all Dismiss Buttonを押すとスタート画面、RetryButtonを押すと再びゲーム画面が表示される。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/18 00:31