Swiftでタイマーをつくる1

iPhone6Sを購入したので、swiftでプログラムを書いてみた。 コードはSwift Docsを参考にさせていただきました。 というか、そのままです。新しくプロジェクトを作成したら、UIViewController.swiftのみ編集します。

//
//  ViewController.swift
//  Timer
//

// UIKitライブラリをインポート
import UIKit

// UIViewControllerのクラス
class ViewController: UIViewController {
    
    // タイマーでカウントした値を格納するインスタンスをFloat型で作成する
    var cnt: Float = 0
    // UILabel型のインスタンスを作成する
    var myLabel: UILabel!
    // タイマー
    var timer: NSTimer!
    

    override func viewDidLoad() {

        super.viewDidLoad()
        
        // ラベルを作成
        // ラベルの位置と大きさを指定する
        myLabel = UILabel(frame: CGRectMake(0, 0, 200, 50))
        // UILabの背景色プロパティを白にする
        myLabel.backgroundColor = UIColor.whiteColor()
        myLabel.layer.masksToBounds = true
        // ラベルの角丸プロパティを10.0に設定する
        myLabel.layer.cornerRadius = 10.0
        // ラベルのテキストプロパティに"Time"という文字とタイマーでカウントした数値を表示する
        myLabel.text = "Time:\(cnt)"
        // ラベルの文字の色を黒にする
        myLabel.textColor = UIColor.blackColor()
        // ラベルの影をグレーでつける
        myLabel.shadowColor = UIColor.grayColor()
        // ラベルのテキストの位置を中心にする
        myLabel.textAlignment = NSTextAlignment.Center
        // ラベルの位置を画面の中央の位置に設定する
        myLabel.layer.position = CGPoint(x: self.view.center.x, y: self.view.center.y - 150)
        // ビューの背景色を設定する
        self.view.backgroundColor = UIColor.init(red: 100, green: 1, blue: 10, alpha: 0.5)
        // ビューにラベルを追加する
        self.view.addSubview(myLabel)
        
        
        // タイマー停止用ボタン
        let myButton = UIButton(frame: CGRectMake(0, 0, 200, 50))
        myButton.layer.masksToBounds = true
        myButton.layer.cornerRadius = 20.0
        myButton.backgroundColor = UIColor.blueColor()
        myButton.layer.position = CGPointMake(self.view.center.x, self.view.center.y + 150)
        myButton.setTitle("Stop Timer", forState: UIControlState.Normal)
        myButton.addTarget(self, action: "onMyButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(myButton)
        
        
        // タイマーをつくる
        // 0.1秒単位で時間を計測するタイマーを作成する
        timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "onUpdate:", userInfo: nil, repeats: true)
    }
    
    // ボタンが押されたら呼び出されるメソッド
    func onMyButtonClick(sender: UIButton) {
        // タイマーが動いているなら
        if timer.valid == true {
            // タイマーを破棄する
            timer.invalidate()
            // ボタンのタイトルを変更
            sender.setTitle("Start Timer", forState: UIControlState.Normal)
        } else {
            // タイマー起動
            timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "onUpdate:", userInfo: nil, repeats: true)
            // タイマーのタイトルを変更
            sender.setTitle("Stop Timer", forState: UIControlState.Normal)
        }
    }
    
    // NSTimer.scheduledTimerWithTimeIntervalで指定した秒数毎に呼び出すメソッドをつくる
    func onUpdate(timer: NSTimer) {
        // onUpdateメソッドが呼ばれる毎にcntに0.1追加する
        cnt += 0.1
        // 変数を用意し、文字列"Time"と小数点第一位まで含めたcntの数値を合体させて格納する
        let str = "Time:".stringByAppendingFormat("%.1f", cnt)
        // onUpdateメソッドが呼ばれる毎にラベルのテキストプロパティに変数strを代入する
        myLabel.text = str
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}