質問編集履歴

1

コードを全て載せました。

2022/01/20 08:38

投稿

vangogh---
vangogh---

スコア4

test CHANGED
File without changes
test CHANGED
@@ -2,16 +2,106 @@
2
2
 
3
3
 
4
4
  ```ここに言語を入力
5
+ import UIKit
6
+ import AVFoundation
7
+
8
+ class QuizViewController: UIViewController {
9
+
10
+ @IBOutlet var quizNumberLabel:UILabel!
11
+ @IBOutlet var quizTextView:UITextView!
12
+ @IBOutlet var answerButton1:UIButton!
13
+ @IBOutlet var answerButton2:UIButton!
14
+ @IBOutlet var answerButton3:UIButton!
15
+ @IBOutlet var answerButton4:UIButton!
16
+
17
+ var csvArray: [String] = []
18
+ var quizArray: [String] = []
19
+ var quizCount = 0
20
+ var quizCountCorrect = 0
5
- var alarm = Alarm()
21
+ let alarm = Alarm()
22
+
23
+
24
+ override func viewDidLoad() {
25
+ super.viewDidLoad()
26
+
27
+ csvArray = loadCSV(fileName: "quiz")
28
+
29
+ quizArray = csvArray[quizCount].components(separatedBy: ",")
30
+
31
+ quizNumberLabel.text = "第\(quizCount + 1)問"
32
+ quizTextView.text = quizArray[0]
33
+ answerButton1.setTitle(quizArray[2], for: .normal)
34
+ answerButton2.setTitle(quizArray[3], for: .normal)
35
+ answerButton3.setTitle(quizArray[4], for: .normal)
36
+ answerButton4.setTitle(quizArray[5], for: .normal)
37
+
38
+ // Do any additional setup after loading the view.
39
+ }
40
+
41
+ @IBAction func btnAction(sender: UIButton) {
42
+ if sender.tag == Int(quizArray[1]) {
43
+ print("正解")
44
+ quizCountCorrect += 1
45
+ } else {
46
+ print("不正解")
47
+ quizCountCorrect = 0
48
+ }
49
+ nextQuiz()
50
+ }
51
+
52
+ func nextQuiz() {
53
+ quizCount += 1
54
+ quizArray = csvArray[quizCount].components(separatedBy: ",")
55
+ quizNumberLabel.text = "第\(quizCount + 1)問"
56
+ quizTextView.text = quizArray[0]
57
+ answerButton1.setTitle(quizArray[2], for: .normal)
58
+ answerButton2.setTitle(quizArray[3], for: .normal)
59
+ answerButton3.setTitle(quizArray[4], for: .normal)
60
+ answerButton4.setTitle(quizArray[5], for: .normal)
61
+
6
- if quizCountCorrect == 3 {
62
+ if quizCountCorrect == 3 {
7
- alarm.stopTimer()
63
+ alarm.stopTimer()
8
- alarm.audioPlayer.numberOfLoops = 0
64
+ alarm.audioPlayer.numberOfLoops = 0
9
- performSegue(withIdentifier: "GoodMorning", sender: nil)
65
+ performSegue(withIdentifier: "GoodMorning", sender: nil)
10
- }
66
+ }
67
+
68
+ }
69
+
70
+ func loadCSV(fileName: String) -> [String] {
71
+ let csvBundle = Bundle.main.path(forResource: fileName, ofType: "csv")!
72
+ do {
73
+ let csvData = try String(contentsOfFile: csvBundle, encoding: String.Encoding.utf8)
74
+ let lineChange = csvData.replacingOccurrences(of: "\r", with: "\n")
75
+ csvArray = lineChange.components(separatedBy: "\n")
76
+ csvArray.removeLast()
77
+ } catch {
78
+ print("エラー")
79
+ }
80
+ return csvArray
81
+ }
82
+
11
83
  ```
12
84
  ちなみにAlarmファイルの一部を貼っておきます。
13
85
  ```ここに言語を入力
86
+ import AVFoundation
87
+ import UIKit
88
+
89
+ class Alarm{
90
+
91
+ var selectedWakeUpTime:Date?
92
+ var audioPlayer: AVAudioPlayer!
93
+ var sleepTimer: Timer?
94
+ var seconds = 0
95
+
96
+ func runTimer(){
97
+ seconds = calculateInterval(userAwakeTime: selectedWakeUpTime!)
98
+ guard sleepTimer == nil else { return }
99
+ if sleepTimer == nil{
100
+ sleepTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
101
+ }
102
+ }
103
+
14
- @objc private func updateTimer(){
104
+ @objc private func updateTimer(){
15
105
  if seconds != 0{
16
106
  seconds -= 1
17
107
 
@@ -34,6 +124,24 @@
34
124
  print(audioPlayer.isPlaying)
35
125
  }
36
126
  }
127
+
128
+ func stopTimer(){
129
+ if sleepTimer != nil {
130
+ sleepTimer!.invalidate()
131
+ sleepTimer = nil
132
+ }
133
+ }
134
+
135
+ private func calculateInterval(userAwakeTime:Date)-> Int{
136
+ var interval = Int(userAwakeTime.timeIntervalSinceNow)
137
+ if interval < 0{
138
+ interval = 86400 - (0 - interval)
139
+ }
140
+ let calendar = Calendar.current
141
+ let seconds = calendar.component(.second, from: userAwakeTime)
142
+ return interval - seconds
143
+ }
144
+ }
37
145
  ```
38
146
 
39
147
  回答いただけると嬉しいです。質問には何でも答えます。よろしくお願いします。