質問編集履歴
1
投稿失敗したので再送
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,25 +1,63 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
+
swift5でアプリを作成しています。目的は、特定の時間になるとタイマーが作動してカウントダウンを始めるようにしたいです。条件分岐を用いて実現しようとしています。しかし、時間を指定する条件分岐の書き方がわかりません。どうすればいいでしょうか?
|
2
3
|
|
4
|
+
ソースコード
|
5
|
+
//
|
6
|
+
// ViewController.swift
|
3
|
-
|
7
|
+
// clock5
|
8
|
+
//
|
4
|
-
|
9
|
+
// Created by 吉崎敦志 on 2021/01/30.
|
5
|
-
|
10
|
+
//
|
6
11
|
|
12
|
+
import UIKit
|
7
|
-
|
13
|
+
import Foundation
|
8
14
|
|
9
|
-
```
|
10
|
-
|
15
|
+
let cal = Calendar(identifier: .gregorian)
|
11
|
-
```
|
12
16
|
|
13
|
-
|
17
|
+
let tooDate = Date(timeIntervalSinceReferenceDate: +634726800+9000)
|
14
18
|
|
15
|
-
|
19
|
+
let toDate = Date(timeIntervalSinceReferenceDate: +634726800)
|
16
|
-
ソースコード
|
17
|
-
```
|
18
20
|
|
19
|
-
|
21
|
+
let startDate = Date()
|
20
22
|
|
21
|
-
|
23
|
+
let time1 = cal.dateComponents([.second], from: startDate, to: toDate)
|
22
24
|
|
23
|
-
### 補足情報(FW/ツールのバージョンなど)
|
24
25
|
|
26
|
+
class ViewController: UIViewController {
|
27
|
+
|
28
|
+
@IBOutlet weak var timerLabel: UILabel!
|
29
|
+
|
30
|
+
override func viewDidLoad() {
|
31
|
+
super.viewDidLoad()
|
32
|
+
// Do any additional setup after loading the view.
|
33
|
+
|
34
|
+
timerLabel.text = ""
|
35
|
+
|
36
|
+
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.timerCounter), userInfo: nil, repeats: true)
|
37
|
+
|
38
|
+
}
|
39
|
+
|
40
|
+
@objc func timerCounter(){
|
41
|
+
if 1 > 0 {
|
42
|
+
timerLabel.text =
|
43
|
+
TimerFunction(setDate: toDate)
|
44
|
+
}
|
45
|
+
|
46
|
+
}
|
47
|
+
|
48
|
+
func TimerFunction(setDate: Date) -> String {
|
49
|
+
|
25
|
-
|
50
|
+
var nowDate = Date()
|
51
|
+
|
52
|
+
let calendar = Calendar(identifier: .japanese)
|
53
|
+
let timeValue = calendar
|
54
|
+
.dateComponents([.day, .hour, .minute, .second], from: nowDate, to: setDate)
|
55
|
+
|
56
|
+
return String(format:
|
57
|
+
"残り"+"%02d時間:%02d分:%02d秒",
|
58
|
+
timeValue.hour!,
|
59
|
+
timeValue.minute!,
|
60
|
+
timeValue.second!)
|
61
|
+
}
|
62
|
+
|
63
|
+
}
|