質問編集履歴
2
書式の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
![イメージ説明](fd760e2d8197430c1130a89bff3f45dd.png)
|
2
|
-
|
3
|
-
|
4
|
-
|
5
1
|
**やりたいこと**
|
6
2
|
|
7
3
|
pickerviewを使ってタイマーをカウントダウンできるようにしたい
|
1
書式の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,259 +1,3 @@
|
|
1
|
-
```swift
|
2
|
-
|
3
|
-
import UIKit
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@IBOutlet weak var label: UILabel!
|
12
|
-
|
13
|
-
@IBOutlet weak var pickerView: UIPickerView!
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
//タイマー変数
|
18
|
-
|
19
|
-
var timer = Timer()
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
//カウント(経過時間)
|
24
|
-
|
25
|
-
var count: Double = 0.0
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
// 選択肢
|
32
|
-
|
33
|
-
var dataList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
|
34
|
-
|
35
|
-
var dataList2 = ["00", "15", "30", "45"]
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
override func viewDidLoad() {
|
40
|
-
|
41
|
-
super.viewDidLoad()
|
42
|
-
|
43
|
-
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
48
|
-
|
49
|
-
// 表示する列数
|
50
|
-
|
51
|
-
return 2
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
58
|
-
|
59
|
-
// アイテム表示個数を返す
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
if component == 0 {
|
64
|
-
|
65
|
-
// 1個目のピッカーの設定
|
66
|
-
|
67
|
-
return dataList.count
|
68
|
-
|
69
|
-
}
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
return dataList2.count
|
74
|
-
|
75
|
-
}
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
// UIPickerViewDelegate
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
|
86
|
-
|
87
|
-
// 表示する文字列を返す
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
if component == 0 {
|
92
|
-
|
93
|
-
// 1個目のピッカーの設定
|
94
|
-
|
95
|
-
return dataList[row]
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
return dataList2[row]
|
102
|
-
|
103
|
-
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
108
|
-
|
109
|
-
// 選択時の処理
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
let minute = dataList[pickerView.selectedRow(inComponent: 0)]
|
114
|
-
|
115
|
-
let second = dataList2[pickerView.selectedRow(inComponent: 1)]
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
print("\(minute):\(second)")
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
label.text = "\(minute):\(second)"
|
124
|
-
|
125
|
-
}
|
126
|
-
|
127
|
-
@IBAction func startbutton(_ sender: Any) {
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
//すでに動いているタイマーは停止する
|
132
|
-
|
133
|
-
timer.invalidate()
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
dataList = ["00:00"]
|
138
|
-
|
139
|
-
dataList2 = ["00:00"]
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
//1秒周期でcountDownメソッドを呼び出すタイマーを開始する。
|
144
|
-
|
145
|
-
timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector:#selector(ViewController.countDown), userInfo:nil, repeats:true)
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
@IBAction func finishbutton(_ sender: Any) {
|
152
|
-
|
153
|
-
}
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
//タイマーから呼び出されるメソッド
|
158
|
-
|
159
|
-
func countDown(){
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
//カウントを減らす。
|
164
|
-
|
165
|
-
count -= 1
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
print("\(dataList):\(dataList2)")
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
//カウントダウン状況をラベルに表示
|
174
|
-
|
175
|
-
if(count > 0) {
|
176
|
-
|
177
|
-
label.text = "\(dataList):\(dataList2)"
|
178
|
-
|
179
|
-
} else {
|
180
|
-
|
181
|
-
label.text = "カウントダウン終了"
|
182
|
-
|
183
|
-
timer.invalidate()
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
func updataLabel() {
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
func updateLabel() {
|
196
|
-
|
197
|
-
// 経過した時間を、現在の時刻-開始時刻で算出(秒)
|
198
|
-
|
199
|
-
let elapsedTime = Date().timeIntervalSince1970 - count
|
200
|
-
|
201
|
-
// 小数点以下を切り捨てる
|
202
|
-
|
203
|
-
let flooredErapsedTime = Int(floor(elapsedTime))
|
204
|
-
|
205
|
-
// 残り時間
|
206
|
-
|
207
|
-
let leftTime = 5 - flooredErapsedTime
|
208
|
-
|
209
|
-
// ラベルに表示する残り時間
|
210
|
-
|
211
|
-
let displayString = NSString(format: "00:%02d", leftTime) as String
|
212
|
-
|
213
|
-
// ラベルを更新
|
214
|
-
|
215
|
-
dataList = [displayString]
|
216
|
-
|
217
|
-
dataList2 = [displayString]
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
// 残り0秒になった時の処理
|
222
|
-
|
223
|
-
if leftTime == 0 {
|
224
|
-
|
225
|
-
// タイマーを止める
|
226
|
-
|
227
|
-
timer.invalidate()
|
228
|
-
|
229
|
-
// アラートを表示する
|
230
|
-
|
231
|
-
let alert = UIAlertController(title: "完了", message: "終了しました", preferredStyle: .alert)
|
232
|
-
|
233
|
-
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
|
234
|
-
|
235
|
-
alert.addAction(okAction)
|
236
|
-
|
237
|
-
present(alert, animated: true, completion: nil)
|
238
|
-
|
239
|
-
/*// ボタンを押下できるようにする
|
240
|
-
|
241
|
-
button.isEnabled = true*/
|
242
|
-
|
243
|
-
}
|
244
|
-
|
245
|
-
}
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
```
|
254
|
-
|
255
|
-
|
256
|
-
|
257
1
|
![イメージ説明](fd760e2d8197430c1130a89bff3f45dd.png)
|
258
2
|
|
259
3
|
|