質問編集履歴

1

コードを書き足しました

2017/12/21 18:04

投稿

SKMT
SKMT

スコア57

test CHANGED
File without changes
test CHANGED
@@ -19,6 +19,8 @@
19
19
  weak var timer: Timer!
20
20
 
21
21
  var startTime = Date()
22
+
23
+ var tmp = ["00","00","00"]
22
24
 
23
25
 
24
26
 
@@ -46,7 +48,7 @@
46
48
 
47
49
  timer = Timer.scheduledTimer(
48
50
 
49
- timeInterval: 0.1,
51
+ timeInterval: 0.01,
50
52
 
51
53
  target: self,
52
54
 
@@ -57,6 +59,12 @@
57
59
  repeats: true)
58
60
 
59
61
 
62
+
63
+ timerMinute.text = tmp[0]
64
+
65
+ timerSecond.text = tmp[1]
66
+
67
+ timerMSec.text = tmp[2]
60
68
 
61
69
 
62
70
 
@@ -72,6 +80,14 @@
72
80
 
73
81
  }
74
82
 
83
+
84
+
85
+ tmp[0] = timerMinute.text!
86
+
87
+ tmp[1] = timerSecond.text!
88
+
89
+ tmp[2] = timerMSec.text!
90
+
75
91
  }
76
92
 
77
93
 
@@ -83,6 +99,70 @@
83
99
  timerSecond.text = "00"
84
100
 
85
101
  timerMSec.text = "00"
102
+
103
+
104
+
105
+ tmp[0] = timerMinute.text!
106
+
107
+ tmp[1] = timerSecond.text!
108
+
109
+ tmp[2] = timerMSec.text!
110
+
111
+ }
112
+
113
+
114
+
115
+
116
+
117
+ @objc func timerCounter() {
118
+
119
+ // タイマー開始からのインターバル時間
120
+
121
+ let currentTime = Date().timeIntervalSince(startTime)
122
+
123
+
124
+
125
+ // fmod() 余りを計算
126
+
127
+ let minute = (Int)(fmod((currentTime/60), 60))
128
+
129
+ // currentTime/60 の余り
130
+
131
+ let second = (Int)(fmod(currentTime, 60))
132
+
133
+ // floor 切り捨て、小数点以下を取り出して *100
134
+
135
+ let msec = (Int)((currentTime - floor(currentTime))*100)
136
+
137
+
138
+
139
+ // %02d: 2桁表示、0で埋める
140
+
141
+ let sMinute = String(format:"%02d", minute)
142
+
143
+ let sSecond = String(format:"%02d", second)
144
+
145
+ let sMsec = String(format:"%02d", msec)
146
+
147
+
148
+
149
+ timerMinute.text = sMinute
150
+
151
+ timerSecond.text = sSecond
152
+
153
+ timerMSec.text = sMsec
154
+
155
+
156
+
157
+ }
158
+
159
+
160
+
161
+ override func viewWillDisappear(_ animated: Bool) {
162
+
163
+ super.viewWillDisappear(true)
164
+
165
+ timer.invalidate()
86
166
 
87
167
  }
88
168