質問編集履歴
1
コードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -47,3 +47,503 @@
|
|
47
47
|
昨日と今日、頑張りましたが自力では無理なようです。
|
48
48
|
|
49
49
|
どなたかご教授お願いします。
|
50
|
+
|
51
|
+
```
|
52
|
+
|
53
|
+
//
|
54
|
+
|
55
|
+
// ViewController.swift
|
56
|
+
|
57
|
+
// testTimer
|
58
|
+
|
59
|
+
//
|
60
|
+
|
61
|
+
// Created by MBP13 on 2019/05/01.
|
62
|
+
|
63
|
+
// Copyright © 2019 Tomoyuki Ashikari. All rights reserved.
|
64
|
+
|
65
|
+
//
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
import UIKit
|
72
|
+
|
73
|
+
import AudioToolbox //バイブ用
|
74
|
+
|
75
|
+
import GoogleMobileAds
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
class ViewController: UIViewController, GADBannerViewDelegate {
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
var userTimer = 0
|
86
|
+
|
87
|
+
var userSecTimer = 0
|
88
|
+
|
89
|
+
var count = 0.0
|
90
|
+
|
91
|
+
var reUserTime = 0
|
92
|
+
|
93
|
+
var reUserSecTime = 0
|
94
|
+
|
95
|
+
var reCount = 0.0
|
96
|
+
|
97
|
+
var elapsedTime = 0.0
|
98
|
+
|
99
|
+
var c = 0
|
100
|
+
|
101
|
+
var timer = Timer()
|
102
|
+
|
103
|
+
//var startDate = Date() //2019-05-01 12:55:48 +0000で取得
|
104
|
+
|
105
|
+
var stopDate = Date()
|
106
|
+
|
107
|
+
var startDate = Date()
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
var bannerView: GADBannerView!//広告用
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
@IBOutlet weak var minButton: UIButton!
|
118
|
+
|
119
|
+
@IBOutlet weak var secButton: UIButton!
|
120
|
+
|
121
|
+
@IBOutlet weak var reset: UIButton!
|
122
|
+
|
123
|
+
@IBOutlet weak var startButton: UIButton!
|
124
|
+
|
125
|
+
@IBOutlet weak var stopButton: UIButton!
|
126
|
+
|
127
|
+
@IBOutlet weak var timerLabel: UILabel!
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
var secLabel:UILabel!
|
132
|
+
|
133
|
+
var msecLabel:UILabel!
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
func addBannerViewToView(_ bannerView: GADBannerView) {//広告用
|
138
|
+
|
139
|
+
bannerView.translatesAutoresizingMaskIntoConstraints = false//広告用
|
140
|
+
|
141
|
+
view.addSubview(bannerView)//広告用
|
142
|
+
|
143
|
+
view.addConstraints(//広告用
|
144
|
+
|
145
|
+
[NSLayoutConstraint(item: bannerView,//広告用
|
146
|
+
|
147
|
+
attribute: .bottom,//広告用
|
148
|
+
|
149
|
+
relatedBy: .equal,//広告用
|
150
|
+
|
151
|
+
toItem:view.safeAreaLayoutGuide,//広告用
|
152
|
+
|
153
|
+
attribute: .bottom,//広告用
|
154
|
+
|
155
|
+
multiplier: 1,//広告用
|
156
|
+
|
157
|
+
constant: 0),//広告用
|
158
|
+
|
159
|
+
NSLayoutConstraint(item: bannerView,//広告用
|
160
|
+
|
161
|
+
attribute: .centerX,//広告用
|
162
|
+
|
163
|
+
relatedBy: .equal,//広告用
|
164
|
+
|
165
|
+
toItem: view,//広告用
|
166
|
+
|
167
|
+
attribute: .centerX,//広告用
|
168
|
+
|
169
|
+
multiplier: 1,//広告用
|
170
|
+
|
171
|
+
constant: 0)//広告用
|
172
|
+
|
173
|
+
])//広告用
|
174
|
+
|
175
|
+
}//広告用
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
override func viewDidLoad() {
|
180
|
+
|
181
|
+
super.viewDidLoad()
|
182
|
+
|
183
|
+
//startDates配列を再び読み込みset
|
184
|
+
|
185
|
+
if userDefaults.object(forKey: "startDates") != nil {
|
186
|
+
|
187
|
+
startDates = userDefaults.array(forKey: "startDates") as! [Date] //配列startDatesに呼び出し
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)//広告用
|
194
|
+
|
195
|
+
addBannerViewToView(bannerView)//広告用
|
196
|
+
|
197
|
+
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"//広告用
|
198
|
+
|
199
|
+
bannerView.rootViewController = self//広告用
|
200
|
+
|
201
|
+
bannerView.load(GADRequest())//広告用
|
202
|
+
|
203
|
+
bannerView.delegate = self//広告用
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
//バイブ関数
|
210
|
+
|
211
|
+
func vibrate() {
|
212
|
+
|
213
|
+
AudioServicesPlaySystemSound( 1011 )
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
override func viewDidAppear(_ animated: Bool) {
|
220
|
+
|
221
|
+
super.viewDidAppear(animated)
|
222
|
+
|
223
|
+
if count == 0.0 {
|
224
|
+
|
225
|
+
setButtonEnabled(true, true, false, false, false)
|
226
|
+
|
227
|
+
}else if timer.isValid == false { //タイマーが止まっていたら
|
228
|
+
|
229
|
+
setButtonEnabled(false, false, true, false, true)
|
230
|
+
|
231
|
+
}else{
|
232
|
+
|
233
|
+
setButtonEnabled(false, false, false, true, true)
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
// setAdjustsImageWhenDisabled()
|
238
|
+
|
239
|
+
//画面が表示されたらタイマーを動かす
|
240
|
+
|
241
|
+
// createTimer()
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
//タイマーの処理
|
250
|
+
|
251
|
+
@objc func timerAction(sender:Timer){
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
var minCount = Int(count)/60
|
256
|
+
|
257
|
+
var secCount = Int(count) % 60
|
258
|
+
|
259
|
+
var msecCount = Int((count - Double(minCount * 60) - Double(secCount)) * 100.0)
|
260
|
+
|
261
|
+
timerLabel.text = String(format:"%02d:%02d:%02d",minCount,secCount,msecCount)//%02dで整数を表示
|
262
|
+
|
263
|
+
// secLabel.text = String(secCount)
|
264
|
+
|
265
|
+
// msecLabel.text = String(msecCount)
|
266
|
+
|
267
|
+
count -= 0.1
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
if count <= 0.0{
|
272
|
+
|
273
|
+
sender.invalidate() //止める
|
274
|
+
|
275
|
+
setButtonEnabled(true, true, false, false, false)
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
userTimer = 0
|
280
|
+
|
281
|
+
userSecTimer = 0
|
282
|
+
|
283
|
+
elapsedTime = 0
|
284
|
+
|
285
|
+
count = 0
|
286
|
+
|
287
|
+
minCount = 0
|
288
|
+
|
289
|
+
secCount = 0
|
290
|
+
|
291
|
+
msecCount = 0
|
292
|
+
|
293
|
+
timerLabel.text = String(format:"%02d:%02d:%02d",0,0,0)
|
294
|
+
|
295
|
+
vibrate()// バイブ起動
|
296
|
+
|
297
|
+
AudioServicesPlaySystemSoundWithCompletion(1013) {
|
298
|
+
|
299
|
+
//サウンドが鳴り終わった後の処理を記述
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
//再スタート用
|
304
|
+
|
305
|
+
let minCount = Int(self.reCount)/60
|
306
|
+
|
307
|
+
let secCount = Int(self.reCount) % 60
|
308
|
+
|
309
|
+
let msecCount = Int((self.reCount - Double(minCount * 60) - Double(secCount)) * 100.0)
|
310
|
+
|
311
|
+
self.timerLabel.text = String(format:"%02d:%02d:%02d",minCount,secCount,msecCount)//%02dで整数を表示
|
312
|
+
|
313
|
+
setButtonEnabled(true,true,true,false,true)
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
func setButtonEnabled(_ minButton:Bool,_ secButton:Bool,_ startButton:Bool,_ stopButton:Bool,_ reset:Bool){ //bool型の変数をisEnabledに代入
|
322
|
+
|
323
|
+
self.minButton.isEnabled = minButton
|
324
|
+
|
325
|
+
self.secButton.isEnabled = secButton
|
326
|
+
|
327
|
+
self.startButton.isEnabled = startButton
|
328
|
+
|
329
|
+
self.stopButton.isEnabled = stopButton
|
330
|
+
|
331
|
+
self.reset.isEnabled = reset
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
//非選択の色を変えない、ボタンの設定でカスタムにしないと機能しない
|
338
|
+
|
339
|
+
// func setAdjustsImageWhenDisabled(){
|
340
|
+
|
341
|
+
// self.minButton.adjustsImageWhenDisabled = false
|
342
|
+
|
343
|
+
// self.secButton.adjustsImageWhenDisabled = false
|
344
|
+
|
345
|
+
// self.startButton.adjustsImageWhenDisabled = false
|
346
|
+
|
347
|
+
// self.stopButton.adjustsImageWhenDisabled = false
|
348
|
+
|
349
|
+
// self.reset.adjustsImageWhenDisabled = false
|
350
|
+
|
351
|
+
// }
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
@IBAction func minButton(_ sender: Any) {
|
358
|
+
|
359
|
+
setButtonEnabled(true, true, true, false, true)
|
360
|
+
|
361
|
+
// setAdjustsImageWhenDisabled()
|
362
|
+
|
363
|
+
if reCount > 0{
|
364
|
+
|
365
|
+
userTimer = reUserTime
|
366
|
+
|
367
|
+
userSecTimer = reUserSecTime
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
userTimer += 1
|
372
|
+
|
373
|
+
reUserTime += 1
|
374
|
+
|
375
|
+
timerLabel.text = String(format:"%02d:%02d:%02d",userTimer,userSecTimer,0)
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
@IBAction func secButton(_ sender: Any) {
|
384
|
+
|
385
|
+
setButtonEnabled(true, true, true, false, true)
|
386
|
+
|
387
|
+
// setAdjustsImageWhenDisabled()
|
388
|
+
|
389
|
+
if reCount > 0{
|
390
|
+
|
391
|
+
userTimer = reUserTime
|
392
|
+
|
393
|
+
userSecTimer = reUserSecTime
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
userSecTimer += 10
|
398
|
+
|
399
|
+
reUserSecTime += 10
|
400
|
+
|
401
|
+
timerLabel.text = String(format:"%02d:%02d:%02d",userTimer,userSecTimer,0)
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
@IBAction func startButton(_ sender: Any) {
|
408
|
+
|
409
|
+
setButtonEnabled(false,false,false,true,false)
|
410
|
+
|
411
|
+
// setAdjustsImageWhenDisabled()
|
412
|
+
|
413
|
+
stopDate=Date()
|
414
|
+
|
415
|
+
// 第1引数は差分を表現する形式を指定します 時間の差を計算する
|
416
|
+
|
417
|
+
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second, .nanosecond], from: startDate, to: stopDate)
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
if Int(dateComponents.second!) <= 2 { //2秒以内は配列に送らずにセットにカウントしない
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
}else{
|
426
|
+
|
427
|
+
//広域配列startDatesに追加していく
|
428
|
+
|
429
|
+
startDates.append(Date())
|
430
|
+
|
431
|
+
userDefaults.set(startDates, forKey:"startDates")//永続化で保存
|
432
|
+
|
433
|
+
// print("startDatesで永続化したデータは")
|
434
|
+
|
435
|
+
// print(startDates)
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
//タイマーの作成
|
440
|
+
|
441
|
+
func createTimer(){
|
442
|
+
|
443
|
+
print(count)
|
444
|
+
|
445
|
+
print(reCount)
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
if reCount > 0 && count == 0{
|
450
|
+
|
451
|
+
reCount = (Double(reUserTime) * 60 + Double(reUserSecTime))
|
452
|
+
|
453
|
+
count = reCount
|
454
|
+
|
455
|
+
}else{
|
456
|
+
|
457
|
+
count = (Double(userTimer) * 60 + Double(userSecTimer) + elapsedTime)
|
458
|
+
|
459
|
+
}
|
460
|
+
|
461
|
+
reCount = (Double(reUserTime) * 60 + Double(reUserSecTime))
|
462
|
+
|
463
|
+
//0になったら元に戻すためのて変数
|
464
|
+
|
465
|
+
self.timer = Timer.scheduledTimer(timeInterval: 0.1,
|
466
|
+
|
467
|
+
target: self,
|
468
|
+
|
469
|
+
selector: #selector(self.timerAction(sender:)),
|
470
|
+
|
471
|
+
userInfo: nil,
|
472
|
+
|
473
|
+
repeats: true)
|
474
|
+
|
475
|
+
// timer.fire()
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
createTimer()
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
@IBAction func stopButton(_ sender: Any) {
|
486
|
+
|
487
|
+
setButtonEnabled(false, false, true, false, true)
|
488
|
+
|
489
|
+
stopDate = Date()
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
userTimer = 0
|
494
|
+
|
495
|
+
userSecTimer = 0
|
496
|
+
|
497
|
+
elapsedTime = count
|
498
|
+
|
499
|
+
self.timer.invalidate()//止める
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
@IBAction func reset(_ sender: Any) {
|
506
|
+
|
507
|
+
setButtonEnabled(true, true, false, false, false)
|
508
|
+
|
509
|
+
// setAdjustsImageWhenDisabled()
|
510
|
+
|
511
|
+
userTimer = 0
|
512
|
+
|
513
|
+
userSecTimer = 0
|
514
|
+
|
515
|
+
reUserTime = 0
|
516
|
+
|
517
|
+
reUserSecTime = 0
|
518
|
+
|
519
|
+
elapsedTime = 0
|
520
|
+
|
521
|
+
count = 0
|
522
|
+
|
523
|
+
reCount = 0
|
524
|
+
|
525
|
+
timerLabel.text = String(format:"%02d:%02d:%02d",userTimer,userSecTimer,0)
|
526
|
+
|
527
|
+
self.timer.invalidate()//止める
|
528
|
+
|
529
|
+
}
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
@IBAction func transitionButton(_ sender: Any) {
|
534
|
+
|
535
|
+
setButtonEnabled(false, false, false, true, true)
|
536
|
+
|
537
|
+
userTimer = 0
|
538
|
+
|
539
|
+
userSecTimer = 0
|
540
|
+
|
541
|
+
elapsedTime = count
|
542
|
+
|
543
|
+
}
|
544
|
+
|
545
|
+
}
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
```
|