LoginSignup
6
9

More than 3 years have passed since last update.

iosアプリで1分タイマーを作ってみた

Posted at

未経験からエンジニアに転職する為アプリをまず一つ作ってみようと思い、1分タイマーアプリを作ってみる事にしました。

バージョン

xcode Version 10.2.1

制作過程

プロジェクトを立ち上げます。
スクリーンショット 2019-07-30 21.11.46.png
Single View APPを使い、次の画面でProductNameなどの必要情報を記載し進める。
スクリーンショット 2019-07-30 21.15.34.png
Main.storyboardにてUILabelを真ん中上に一ヶ所、下部にstart,stop,resetとしてUIButtonを三ヶ所配置する。
スクリーンショット 2019-07-30 21.23.44.png
viewController.swiftファイルにそれぞれを紐付ける。
スクリーンショット 2019-07-30 21.50.30.png
各部品に制約をつける。
今回は簡単にAll Views in View ControllerのAdd Missing Constraintsにて一気に行う。
スクリーンショット 2019-08-01 19.31.28.png
view Controllerにてインスタンスの生成と変数に初期値を代入。

//タイマークラスのインスタンスを生成
var timer = Timer() 
//count変数に初期値0を代入   
var count = 0

スタートボタンの実装

//スタートボタン
@IBAction func timerStartButton(_ sender: Any) {
//invalidateメソッドはオブジェクトからタイマーを削除する
        timer.invalidate()
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector:#selector(ViewController.updateTimer),
        userInfo: nil, repeats: true)
 }

scheduledTimerメソッド各引数の役割り

名前 説明
timeInterval ループなら間隔,1度きりなら発動までの秒数
target メソッドを持つオブジェクト
selector 実行するメソッド
userInfo オブジェクトに付ける値
repeats 繰り返し実行するかどうか

ストップボタンの実装

 //ストップボタン
@IBAction func timerStopButton(_ sendr: Any) {
//invalidateメソッドはオブジェクトからタイマーを削除する
        timer.invalidate()
 }

リセットボタンの実装

//リセットボタン
@IBAction func timerResetButton(_ sender: Any) {
//invalidateメソッドはオブジェクトからタイマーを削除する
        timer.invalidate()
//count変数を0にする
        count = 0
//timerCountLabelのtextプロパティへString型にキャストしたcount変数を代入する(表示させる)
        timerCountLabel.text = String(count)
 } 

委譲される側の関数

//移譲される側のメソッド
@objc func updateTimer() {
//countが60に達するまで1ずつ加算されていく
    if count < 60 {
    count += 1
//timerCountLabelのtextプロパティへString型にキャストしたcount変数を代入する(表示させる)
   timerCountLabel.text = String(count)
}

完成

import UIKit

class ViewController: UIViewController {
    //Timerクラスのインスタンスを作成
    var timer = Timer()
    //countに初期値を設定
    var count = 0
    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    }
    //ラベル
    @IBOutlet weak var timerCountLabel: UILabel!
    //スタートボタン
    @IBAction func timerStartButton(_ sender: Any) {
        timer.invalidate()
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true)
    }
    //ストップボタン
    @IBAction func timerStopButton(_ sendr: Any) {
           timer.invalidate()
        }
    //リセットボタン
    @IBAction func timerResetButton(_ sender: Any) {
        timer.invalidate()
        count = 0
        timerCountLabel.text = String(count)
    }
    //移譲される側の関数
    @objc func updateTimer() {
        if count < 60 {
        count += 1
        timerCountLabel.text = String(count)
    }

    }
}

参考サイト
Swift4環境でTimerの使い方と挙動を確認してみた

6
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9