質問編集履歴
1
投稿失敗したので再送
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,49 +1,125 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
+
|
3
|
+
swift5でアプリを作成しています。目的は、特定の時間になるとタイマーが作動してカウントダウンを始めるようにしたいです。条件分岐を用いて実現しようとしています。しかし、時間を指定する条件分岐の書き方がわかりません。どうすればいいでしょうか?
|
2
4
|
|
3
5
|
|
4
6
|
|
5
|
-
|
7
|
+
ソースコード
|
6
8
|
|
7
|
-
|
9
|
+
//
|
8
10
|
|
9
|
-
|
11
|
+
// ViewController.swift
|
12
|
+
|
13
|
+
// clock5
|
14
|
+
|
15
|
+
//
|
16
|
+
|
17
|
+
// Created by 吉崎敦志 on 2021/01/30.
|
18
|
+
|
19
|
+
//
|
10
20
|
|
11
21
|
|
12
22
|
|
23
|
+
import UIKit
|
24
|
+
|
13
|
-
|
25
|
+
import Foundation
|
14
26
|
|
15
27
|
|
16
28
|
|
17
|
-
```
|
18
|
-
|
19
|
-
|
29
|
+
let cal = Calendar(identifier: .gregorian)
|
20
|
-
|
21
|
-
```
|
22
30
|
|
23
31
|
|
24
32
|
|
25
|
-
|
33
|
+
let tooDate = Date(timeIntervalSinceReferenceDate: +634726800+9000)
|
26
34
|
|
27
35
|
|
28
36
|
|
29
|
-
|
37
|
+
let toDate = Date(timeIntervalSinceReferenceDate: +634726800)
|
30
|
-
|
31
|
-
ソースコード
|
32
|
-
|
33
|
-
```
|
34
38
|
|
35
39
|
|
36
40
|
|
37
|
-
|
41
|
+
let startDate = Date()
|
38
42
|
|
39
43
|
|
40
44
|
|
41
|
-
|
45
|
+
let time1 = cal.dateComponents([.second], from: startDate, to: toDate)
|
42
46
|
|
43
47
|
|
44
48
|
|
49
|
+
|
50
|
+
|
45
|
-
|
51
|
+
class ViewController: UIViewController {
|
46
52
|
|
47
53
|
|
48
54
|
|
55
|
+
@IBOutlet weak var timerLabel: UILabel!
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
override func viewDidLoad() {
|
60
|
+
|
61
|
+
super.viewDidLoad()
|
62
|
+
|
63
|
+
// Do any additional setup after loading the view.
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
timerLabel.text = ""
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.timerCounter), userInfo: nil, repeats: true)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
@objc func timerCounter(){
|
80
|
+
|
81
|
+
if 1 > 0 {
|
82
|
+
|
83
|
+
timerLabel.text =
|
84
|
+
|
85
|
+
TimerFunction(setDate: toDate)
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
func TimerFunction(setDate: Date) -> String {
|
96
|
+
|
97
|
+
|
98
|
+
|
49
|
-
|
99
|
+
var nowDate = Date()
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
let calendar = Calendar(identifier: .japanese)
|
104
|
+
|
105
|
+
let timeValue = calendar
|
106
|
+
|
107
|
+
.dateComponents([.day, .hour, .minute, .second], from: nowDate, to: setDate)
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
return String(format:
|
112
|
+
|
113
|
+
"残り"+"%02d時間:%02d分:%02d秒",
|
114
|
+
|
115
|
+
timeValue.hour!,
|
116
|
+
|
117
|
+
timeValue.minute!,
|
118
|
+
|
119
|
+
timeValue.second!)
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
}
|