質問編集履歴

1

コードとコメントを書き足してわかりやすいようにしました!

2017/12/29 19:48

投稿

SKMT
SKMT

スコア57

test CHANGED
@@ -1 +1 @@
1
- [swift]アラー処理
1
+ [swift]計算した回数のカウンとリセット
test CHANGED
@@ -1,229 +1,503 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- 処理中にcntがある値の時にアラート表示、OKをタップしたらまた元の処理を行わせたい
3
+ cntをく動作させたい
4
4
 
5
5
 
6
6
 
7
7
  ###発生している問題・エラーメッセージ
8
8
 
9
+ cntでカウントしてそれに準じて処理を行なっているのですが
10
+
9
- OKタップしてもまたすぐにアラートが表示されタイマーなんどもリセットされしまいます
11
+ なぜか動作がおかしく、cntを表示させると値おかしく困っています
12
+
13
+
14
+
15
+
16
+
17
+ ###該当のソースコード
18
+
19
+ ```ここに言語を入力
20
+
21
+ import UIKit
22
+
23
+ import CoreMotion
24
+
25
+ import AudioToolbox
26
+
27
+ import AVFoundation
28
+
29
+
30
+
31
+ class ViewController: UIViewController {
32
+
33
+ let motionManager = CMMotionManager()
34
+
35
+ var dataX:[Double] = []
36
+
37
+ var dataY:[Double] = []
38
+
39
+ var dataZ:[Double] = []
40
+
41
+ var deviY = [Double](repeating: 0.0, count: 15000)
42
+
43
+ var dispY = [Double](repeating: 0.0, count: 15000)
44
+
45
+ var btn = false
46
+
47
+ weak var timer: Timer!
48
+
49
+ var startTime = Date()
50
+
51
+ var totalTime = 0.0
52
+
53
+ var cnt:Int = 0
54
+
55
+ var alertY = true
56
+
57
+
58
+
59
+ @IBOutlet weak var timeL: UILabel!
60
+
61
+ @IBOutlet weak var accelX: UILabel!
62
+
63
+ @IBOutlet weak var accelY: UILabel!
64
+
65
+ @IBOutlet weak var accelZ: UILabel!
66
+
67
+
68
+
69
+ @IBOutlet weak var kakuninL: UILabel!
70
+
71
+
72
+
73
+ // 計測開始・停止ボタン
74
+
75
+ @IBAction func startB(_ sender: Any) {
76
+
77
+ let button = sender as! UIButton
78
+
79
+
80
+
81
+ if (btn == true) {
82
+
83
+ btn = false
84
+
85
+ cnt = 0
86
+
87
+ deviY = [Double](repeating: 0.0, count: 15000)
88
+
89
+ dispY = [Double](repeating: 0.0, count: 15000)
90
+
91
+ button.setTitle("計測開始", for: .normal)
92
+
93
+ stopTimer()
94
+
95
+ }
96
+
97
+ else {
98
+
99
+ btn = true
100
+
101
+ button.setTitle("計測停止", for: .normal)
102
+
103
+ startTimer()
104
+
105
+ cnt = 0
106
+
107
+ }
108
+
109
+ }
110
+
111
+
112
+
113
+ @IBAction func resetB(_ sender: Any) {
114
+
115
+ cnt = 0
116
+
117
+ deviY = [Double](repeating: 0.0, count: 15000)
118
+
119
+ dispY = [Double](repeating: 0.0, count: 15000)
120
+
121
+ resetTimer()
122
+
123
+ stopTimer()
124
+
125
+ }
126
+
127
+
128
+
129
+
130
+
131
+ // データの格納
132
+
133
+ func outputAccelData(acceleration: CMAcceleration){
134
+
135
+ if (btn) {
136
+
137
+ dataX.append(acceleration.x)
138
+
139
+ dataY.append(acceleration.y)
140
+
141
+ dataZ.append(acceleration.z)
142
+
143
+
144
+
145
+ accelX.text = String(format: "x = %.3f", acceleration.x)
146
+
147
+ accelY.text = String(format: "y = %.3f", acceleration.y)
148
+
149
+ accelZ.text = String(format: "z = %.3f", acceleration.z)
150
+
151
+ culculator()
152
+
153
+ }
154
+
155
+ }
156
+
157
+
158
+
159
+ // データの計算
160
+
161
+ func culculator() {
162
+
163
+ var tmpy:Double = 0.0
164
+
165
+
166
+
167
+ if(dataY.count > 10) {
168
+
169
+ for i in 0 ..< (dataY.count - 11) {
170
+
171
+ for j in i ..< (i + 10) {
172
+
173
+ tmpy = tmpy + dataY[j]
174
+
175
+ }
176
+
177
+ tmpy = tmpy / 10.0
178
+
179
+
180
+
181
+ for k in i ..< (i + 10) {
182
+
183
+ dispY[i] = dispY[i] + pow(dataY[k]-tmpy,2.0)
184
+
185
+ }
186
+
187
+ dispY[i] = dispY[i] / 10.0
188
+
189
+ deviY[i] = sqrt(dispY[i])
190
+
191
+
192
+
193
+ if(judgecount(deviY[i])){
194
+
195
+ cnt = cnt + 1
196
+
197
+ }
198
+
199
+ }
200
+
201
+
202
+
203
+ }
204
+
205
+ }
206
+
207
+
208
+
209
+ // 動作の判定
210
+
211
+ func judgecount(_ devi:Double) -> Bool {
212
+
213
+ var soundIdRing:SystemSoundID = 1005
214
+
215
+ kakuninL.text = String(format: "%d", cnt)
216
+
217
+
218
+
219
+ if((devi > 0.20)&&(cnt > 50)) {
220
+
221
+ cnt = 0
222
+
223
+ return false
224
+
225
+
226
+
227
+ }
228
+
229
+ else if((cnt == 100)&&(alertY)) {
230
+
231
+ alertY = false
232
+
233
+ let alert1 = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
234
+
235
+ alert1.title = "警告!"
236
+
237
+ alert1.message = "1の方"
238
+
239
+ alert1.addAction(
240
+
241
+ UIAlertAction(
242
+
243
+ title: "OK",
244
+
245
+ style: .default,
246
+
247
+ handler: {(action) -> Void in
248
+
249
+ self.alertY = true
250
+
251
+ }))
252
+
253
+ self.present( alert1, animated: true, completion:nil)
254
+
255
+
256
+
257
+ if let soundUrl = CFBundleCopyResourceURL(CFBundleGetMainBundle(), nil, nil, nil){
258
+
259
+ AudioServicesCreateSystemSoundID(soundUrl, &soundIdRing)
260
+
261
+ AudioServicesPlaySystemSound(soundIdRing)
262
+
263
+ }
264
+
265
+
266
+
267
+ return true
268
+
269
+
270
+
271
+ }
272
+
273
+ else if((cnt > 200)&&(alertY)) {
274
+
275
+ alertY = false
276
+
277
+ let alert2 = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
278
+
279
+ alert2.title = "警告!"
280
+
281
+ alert2.message = "2の方"
282
+
283
+ alert2.addAction(
284
+
285
+ UIAlertAction(
286
+
287
+ title: "OK",
288
+
289
+ style: .default,
290
+
291
+ handler: {(action) -> Void in
292
+
293
+ self.cnt = 0
294
+
295
+ self.alertY = true
296
+
297
+ }))
298
+
299
+ self.present( alert2, animated: true, completion:nil)
300
+
301
+ return false
302
+
303
+ }
304
+
305
+ else {
306
+
307
+ return true
308
+
309
+
310
+
311
+ }
312
+
313
+
314
+
315
+ }
316
+
317
+
318
+
319
+ // アラートの表示
320
+
321
+ func displayalert(_ message:String) {
322
+
323
+ let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
324
+
325
+ alert.title = "警告!"
326
+
327
+ alert.message = message
328
+
329
+ alert.addAction(
330
+
331
+ UIAlertAction(
332
+
333
+ title: "OK",
334
+
335
+ style: .default,
336
+
337
+ handler: {(action) -> Void in
338
+
339
+ self.startTimer()
340
+
341
+ self.alertY = true
342
+
343
+ }))
344
+
345
+ self.present( alert, animated: true, completion:nil)
346
+
347
+ }
348
+
349
+
350
+
351
+ // 時間の表示
352
+
353
+ func displayTime(_ time: TimeInterval) {
354
+
355
+ let minute = (Int)(fmod((time/60), 60))
356
+
357
+ let second = (Int)(fmod(time, 60))
358
+
359
+ let msec = (Int)((time - floor(time))*100)
360
+
361
+
362
+
363
+ timeL.text = String(format: "%02d : %02d : %02d", minute, second, msec)
364
+
365
+ }
366
+
367
+
368
+
369
+ // 時間の計測
370
+
371
+ @objc func timerCounter() {
372
+
373
+ let currentTime = totalTime + Date().timeIntervalSince(startTime)
374
+
375
+
376
+
377
+ displayTime(currentTime)
378
+
379
+ }
380
+
381
+
382
+
383
+ // タイマーのスタート
384
+
385
+ func startTimer() {
386
+
387
+ if timer != nil {
388
+
389
+ timer.invalidate()
390
+
391
+ }
392
+
393
+ else {
394
+
395
+ startTime = Date();
396
+
397
+ }
398
+
399
+ timer = Timer.scheduledTimer(
400
+
401
+ timeInterval: 0.01,
402
+
403
+ target: self,
404
+
405
+ selector: #selector(self.timerCounter),
406
+
407
+ userInfo: nil,
408
+
409
+ repeats: true)
410
+
411
+ }
412
+
413
+
414
+
415
+ // タイマーの一時停止
416
+
417
+ func stopTimer() {
418
+
419
+ if timer != nil {
420
+
421
+ totalTime = totalTime + Date().timeIntervalSince(startTime)
422
+
423
+ timer.invalidate()
424
+
425
+ timer = nil
426
+
427
+ }
428
+
429
+ displayTime(totalTime)
430
+
431
+ }
432
+
433
+
434
+
435
+ // タイマーのリセット
436
+
437
+ func resetTimer() {
438
+
439
+ startTime = Date();
440
+
441
+ totalTime = 0.0
442
+
443
+ displayTime(totalTime)
444
+
445
+ startTimer()
446
+
447
+ }
448
+
449
+
450
+
451
+ // 加速度取得停止
452
+
453
+ func stopAccelerometer(){
454
+
455
+ if (motionManager.isAccelerometerActive) {
456
+
457
+ motionManager.stopAccelerometerUpdates()
458
+
459
+ }
460
+
461
+ }
462
+
463
+
464
+
465
+
466
+
467
+ override func viewDidLoad() {
468
+
469
+ super.viewDidLoad()
470
+
471
+ if motionManager.isAccelerometerAvailable {
472
+
473
+ motionManager.accelerometerUpdateInterval = 0.5
474
+
475
+ motionManager.startAccelerometerUpdates(
476
+
477
+ to: OperationQueue.current!,
478
+
479
+ withHandler: {(accelData: CMAccelerometerData?, errorOC: Error?) in
480
+
481
+ self.outputAccelData(acceleration: accelData!.acceleration)
482
+
483
+ })
484
+
485
+ }
486
+
487
+ UIApplication.shared.isIdleTimerDisabled = true
488
+
489
+ }
490
+
491
+
492
+
493
+ override func didReceiveMemoryWarning() {
494
+
495
+ super.didReceiveMemoryWarning()
496
+
497
+ }
498
+
499
+ }
500
+
501
+
10
502
 
11
503
  ```
12
-
13
- Warning: Attempt to present <UIAlertController: 0x1200f4e00> which is already presenting <UIAlertController: 0x12084d000>
14
-
15
- ```
16
-
17
-
18
-
19
- ###該当のソースコード
20
-
21
- ```ここに言語を入力
22
-
23
- var deviY = [Double](repeating: 0.0, count: 15000)
24
-
25
- var dispY = [Double](repeating: 0.0, count: 15000)
26
-
27
- var btn = false
28
-
29
- weak var timer: Timer!
30
-
31
- var startTime = Date()
32
-
33
- var totalTime = 0.0
34
-
35
- var cnt:Int = 0
36
-
37
-
38
-
39
- // 計測開始・停止ボタン
40
-
41
- @IBAction func startB(_ sender: Any) {
42
-
43
- let button = sender as! UIButton
44
-
45
-
46
-
47
- if (btn == true) {
48
-
49
- btn = false
50
-
51
- button.setTitle("計測開始", for: .normal)
52
-
53
- stopTimer()
54
-
55
- }
56
-
57
- else {
58
-
59
- btn = true
60
-
61
- button.setTitle("計測停止", for: .normal)
62
-
63
- startTimer()
64
-
65
- }
66
-
67
- }
68
-
69
-
70
-
71
- // データの格納
72
-
73
- func outputAccelData(acceleration: CMAcceleration){
74
-
75
- if (btn) {
76
-
77
- dataX.append(acceleration.x)
78
-
79
- dataY.append(acceleration.y)
80
-
81
- dataZ.append(acceleration.z)
82
-
83
-
84
-
85
- culculator()
86
-
87
- }
88
-
89
- }
90
-
91
-
92
-
93
- // データの計算
94
-
95
- func culculator() {
96
-
97
- var tmpy:Double = 0.0
98
-
99
-
100
-
101
- if(dataY.count > 15) {
102
-
103
- for i in 0 ..< (dataY.count - 11) {
104
-
105
- for j in i ..< (i + 10) {
106
-
107
- tmpy = tmpy + dataY[j]
108
-
109
- }
110
-
111
- tmpy = tmpy / 10.0
112
-
113
-
114
-
115
- for k in i ..< (i + 10) {
116
-
117
- dispY[i] = dispY[i] + pow(dataY[k]-tmpy,2.0)
118
-
119
- }
120
-
121
- dispY[i] = dispY[i] / 10.0
122
-
123
- deviY[i] = sqrt(dispY[i])
124
-
125
- judgecount(deviY[i])
126
-
127
- }
128
-
129
-
130
-
131
- }
132
-
133
- }
134
-
135
-
136
-
137
- // 動作の判定
138
-
139
- func judgecount(_ devi:Double) {
140
-
141
- if((devi > 0.10)&&(cnt > 10)) {
142
-
143
- cnt = 0
144
-
145
- resetTimer()
146
-
147
- startTimer()
148
-
149
- }
150
-
151
- else {
152
-
153
- cnt = cnt + 1
154
-
155
-
156
-
157
- if(cnt == 50) {
158
-
159
- displayalert("身体を動かしてください")
160
-
161
- } else if(cnt == 100) {
162
-
163
- displayalert("身体を動かさないと危険です!")
164
-
165
- cnt = 0
166
-
167
- }
168
-
169
- }
170
-
171
-
172
-
173
- }
174
-
175
-
176
-
177
- // 時間の計測
178
-
179
- @objc func timerCounter() {
180
-
181
- let currentTime = totalTime + Date().timeIntervalSince(startTime)
182
-
183
- displayTime(currentTime)
184
-
185
- }
186
-
187
-
188
-
189
- // アラートの表示
190
-
191
- func displayalert(_ message:String) {
192
-
193
- let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
194
-
195
- alert.title = "警告!"
196
-
197
- alert.message = message
198
-
199
- alert.addAction(
200
-
201
- UIAlertAction(
202
-
203
- title: "OK",
204
-
205
- style: .default,
206
-
207
- handler: {(action) -> Void in
208
-
209
- self.stopTimer()
210
-
211
- self.startTimer()
212
-
213
- })
214
-
215
- )
216
-
217
- self.present(
218
-
219
- alert,
220
-
221
- animated: true,
222
-
223
- completion:nil
224
-
225
- )
226
-
227
- }
228
-
229
- ```