質問編集履歴
5
スタイル変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
### コード
|
22
22
|
|
23
|
-
|
23
|
+
下記のコードではセットされた合計時間のカウントダウンの実装+セットされた各インターバルをラベルに表示のみ。そして、終了3秒前からカウントダウンの音源を流すようにしている。
|
24
24
|
|
25
25
|
```
|
26
26
|
|
4
コメントの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,6 +20,8 @@
|
|
20
20
|
|
21
21
|
### コード
|
22
22
|
|
23
|
+
#下記のコードではセットされた合計時間のカウントダウンの実装+セットされた各インターバルをラベルに表示のみ。そして、終了3秒前からカウントダウンの音源を流すようにしている。
|
24
|
+
|
23
25
|
```
|
24
26
|
|
25
27
|
import UIKit
|
3
コードブロック追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,7 +18,411 @@
|
|
18
18
|
|
19
19
|
現在はそれぞれのインターバルを配列に格納し、上記のことを実装できないかと考えています。そのためには配列内の最初の要素(20)に対してTimerをスタートさせ、その要素が0になった時に次のインデックスに移り、それに対応している要素(60)のカウントダウンをスタートさせるようにしたいのですが、そのやり方がリサーチをしても見つかりません。配列での実装に固執しているわけではありませんので、配列に格納しなくとも上記のことが実装することが可能であれば、その方法もご教示いただけると幸いです。
|
20
20
|
|
21
|
-
|
21
|
+
### コード
|
22
|
+
|
23
|
+
```
|
24
|
+
|
25
|
+
import UIKit
|
26
|
+
|
27
|
+
import AVFoundation
|
28
|
+
|
29
|
+
import Foundation
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
@IBOutlet weak var pickerOutlet: UIPickerView!
|
38
|
+
|
39
|
+
@IBOutlet weak var intervalOutlet: UILabel!
|
40
|
+
|
41
|
+
@IBOutlet weak var currentIntervalOutlet: UILabel!
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
@IBOutlet weak var timeOutlet: UILabel!
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
var count = 0
|
50
|
+
|
51
|
+
var audioPlayer = AVAudioPlayer()
|
52
|
+
|
53
|
+
var timer = Timer()
|
54
|
+
|
55
|
+
var intervalTimer = Timer()
|
56
|
+
|
57
|
+
let dataList = [[Int](0...24), [Int](0...60), [Int](0...60)]
|
58
|
+
|
59
|
+
var hourList: [Int] = []
|
60
|
+
|
61
|
+
var minList: [Int] = []
|
62
|
+
|
63
|
+
var secList: [Int] = []
|
64
|
+
|
65
|
+
var intervalList: [String] = []
|
66
|
+
|
67
|
+
var currentIntervalList: [Int] = []
|
68
|
+
|
69
|
+
var totalTime = 0
|
70
|
+
|
71
|
+
var isPause = false
|
72
|
+
|
73
|
+
var isStarted = false
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
override func viewDidLoad() {
|
78
|
+
|
79
|
+
super.viewDidLoad()
|
80
|
+
|
81
|
+
pickerOutlet.delegate = self
|
82
|
+
|
83
|
+
pickerOutlet.dataSource = self
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
do {
|
88
|
+
|
89
|
+
let audioPath = Bundle.main.path(forResource: "sound", ofType: ".mp3")
|
90
|
+
|
91
|
+
try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
catch {
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
104
|
+
|
105
|
+
return dataList.count
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
112
|
+
|
113
|
+
return dataList[component].count
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
|
120
|
+
|
121
|
+
return String(dataList[component][row])
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
func pickerView(_ pickerView: UIPickerView, widthForComponent component:Int) -> CGFloat {
|
126
|
+
|
127
|
+
return pickerOutlet.bounds.width * 1/4
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
@IBOutlet weak var setOutlet: UIButton!
|
138
|
+
|
139
|
+
@IBAction func set(_ sender: UIButton) {
|
140
|
+
|
141
|
+
let selectedRow1 = self.pickerOutlet.selectedRow(inComponent: 0)
|
142
|
+
|
143
|
+
let selectedRow2 = self.pickerOutlet.selectedRow(inComponent: 1)
|
144
|
+
|
145
|
+
let selectedRow3 = self.pickerOutlet.selectedRow(inComponent: 2)
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
let hours = dataList[0][selectedRow1]
|
150
|
+
|
151
|
+
let mins = dataList[1][selectedRow2]
|
152
|
+
|
153
|
+
let secs = dataList[2][selectedRow3]
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
let total = hours * 3600 + mins * 60 + secs
|
158
|
+
|
159
|
+
currentIntervalList.append(total)
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
if (hours != 0 && mins != 0 && secs != 0) {
|
164
|
+
|
165
|
+
let interval = String(hours) + "h" + String(mins) + "m" + String(secs) + "s"
|
166
|
+
|
167
|
+
intervalList.append(interval)
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
else if (hours != 0 && mins != 0 && secs == 0) {
|
172
|
+
|
173
|
+
let interval = String(hours) + "h" + String(mins) + "m"
|
174
|
+
|
175
|
+
intervalList.append(interval)
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
else if (hours != 0 && mins == 0 && secs == 0) {
|
180
|
+
|
181
|
+
let interval = String(hours) + "h"
|
182
|
+
|
183
|
+
intervalList.append(interval)
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
else if (hours == 0 && mins != 0 && secs != 0) {
|
188
|
+
|
189
|
+
let interval = String(mins) + "m" + String(secs) + "s"
|
190
|
+
|
191
|
+
intervalList.append(interval)
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
else if (hours == 0 && mins != 0 && secs == 0) {
|
196
|
+
|
197
|
+
let interval = String(mins) + "m"
|
198
|
+
|
199
|
+
intervalList.append(interval)
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
else if (hours == 0 && mins == 0 && secs != 0) {
|
204
|
+
|
205
|
+
let interval = String(secs) + "s"
|
206
|
+
|
207
|
+
intervalList.append(interval)
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
hourList.append(hours)
|
214
|
+
|
215
|
+
minList.append(mins)
|
216
|
+
|
217
|
+
secList.append(secs)
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
var totalHours = 0;
|
222
|
+
|
223
|
+
for hour in hourList {
|
224
|
+
|
225
|
+
totalHours += hour
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
var totalMins = 0;
|
232
|
+
|
233
|
+
for min in minList {
|
234
|
+
|
235
|
+
totalMins += min
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
var totalSecs = 0;
|
242
|
+
|
243
|
+
for sec in secList {
|
244
|
+
|
245
|
+
totalSecs += sec
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
let hoursDisplayed = String(format: "%02d", totalHours)
|
250
|
+
|
251
|
+
let minsDisplayed = String(format: "%02d", totalMins)
|
252
|
+
|
253
|
+
let secsDisplayed = String(format: "%02d", totalSecs)
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
intervalOutlet.text = intervalList.joined(separator: ",")
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
timeOutlet.text = "(hoursDisplayed) : (minsDisplayed) : (secsDisplayed)"
|
264
|
+
|
265
|
+
startOutlet.isEnabled = true
|
266
|
+
|
267
|
+
backOutlet.isEnabled = true
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
@IBOutlet weak var startOutlet: UIButton!
|
274
|
+
|
275
|
+
@IBAction func start(_ sender: UIButton) {
|
276
|
+
|
277
|
+
if (!isStarted) {
|
278
|
+
|
279
|
+
var hour = 0;
|
280
|
+
|
281
|
+
for h in hourList {
|
282
|
+
|
283
|
+
hour += h
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
var min = 0;
|
290
|
+
|
291
|
+
for m in minList {
|
292
|
+
|
293
|
+
min += m
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
var sec = 0;
|
300
|
+
|
301
|
+
for s in secList {
|
302
|
+
|
303
|
+
sec += s
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
count = hour * 3600 + min * 60 + sec
|
308
|
+
|
309
|
+
isStarted = true
|
310
|
+
|
311
|
+
if !isPause {
|
312
|
+
|
313
|
+
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.counter), userInfo: false, repeats: true)
|
314
|
+
|
315
|
+
startOutlet.setTitle("Pause", for: UIControl.State.normal)
|
316
|
+
|
317
|
+
isPause = true
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
else if isPause {
|
324
|
+
|
325
|
+
timer.invalidate()
|
326
|
+
|
327
|
+
isPause = false
|
328
|
+
|
329
|
+
startOutlet.setTitle("Start", for: UIControl.State.normal)
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
else {
|
336
|
+
|
337
|
+
if !isPause {
|
338
|
+
|
339
|
+
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.counter), userInfo: false, repeats: true)
|
340
|
+
|
341
|
+
startOutlet.setTitle("Pause", for: UIControl.State.normal)
|
342
|
+
|
343
|
+
isPause = true
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
else if isPause {
|
350
|
+
|
351
|
+
timer.invalidate()
|
352
|
+
|
353
|
+
isPause = false
|
354
|
+
|
355
|
+
startOutlet.setTitle("Start", for: UIControl.State.normal)
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
setOutlet.isHidden = true
|
364
|
+
|
365
|
+
backOutlet.isHidden = true
|
366
|
+
|
367
|
+
pickerOutlet.isHidden = true
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
@objc func counter(){
|
374
|
+
|
375
|
+
count -= 1
|
376
|
+
|
377
|
+
var countCopy = count
|
378
|
+
|
379
|
+
var hour = 0
|
380
|
+
|
381
|
+
var min = 0
|
382
|
+
|
383
|
+
var sec = 0
|
384
|
+
|
385
|
+
hour = countCopy / 3600
|
386
|
+
|
387
|
+
countCopy %= 3600
|
388
|
+
|
389
|
+
min = countCopy / 60
|
390
|
+
|
391
|
+
countCopy %= 60
|
392
|
+
|
393
|
+
sec = countCopy
|
394
|
+
|
395
|
+
let Hour = String(format: "%02d", hour)
|
396
|
+
|
397
|
+
let Min = String(format: "%02d", min)
|
398
|
+
|
399
|
+
let Sec = String(format: "%02d", sec)
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
timeOutlet.text = "(Hour) : (Min) : (Sec)"
|
404
|
+
|
405
|
+
if (count == 3) {
|
406
|
+
|
407
|
+
audioPlayer.play()
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
if (count == 0) {
|
412
|
+
|
413
|
+
timer.invalidate()
|
414
|
+
|
415
|
+
startOutlet.isEnabled = false
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
}
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
```
|
22
426
|
|
23
427
|
### 補足情報(FW/ツールのバージョンなど)
|
24
428
|
|
2
画像挿入
test
CHANGED
File without changes
|
test
CHANGED
@@ -27,3 +27,9 @@
|
|
27
27
|
Swift: 4.2
|
28
28
|
|
29
29
|
Xcode: 10.1
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
### 現在のUI
|
34
|
+
|
35
|
+
![イメージ説明](0e03fb7ad4a4a8f1fce19168c5affcc7.png)
|
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,26 +20,6 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
### 該当のソースコード
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
```ここに言語名を入力
|
28
|
-
|
29
|
-
ソースコード
|
30
|
-
|
31
|
-
```
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
### 試したこと
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
ここに問題に対して試したことを記載してください。
|
40
|
-
|
41
|
-
|
42
|
-
|
43
23
|
### 補足情報(FW/ツールのバージョンなど)
|
44
24
|
|
45
25
|
|