質問編集履歴
1
ファイル構成画像追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,39 +4,195 @@
|
|
4
4
|
|
5
5
|
#### 問題
|
6
6
|
|
7
|
-
アラームアプリを作成し実行したのですが、
|
7
|
+
アラームアプリを作成し実行したのですが、実行しても何も表示されません。
|
8
|
-
|
8
|
+
|
9
|
-
|
9
|
+
viewController.swiftを編集しなければならないのでしょうか?
|
10
|
-
|
10
|
+
|
11
|
-
参考にしているサイトには
|
11
|
+
参考にしているサイトには以下の4つのソースコードしかありませんので、ほかをどのようにすれば表示されるのか教えて頂けると幸いです。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
12
|
-
|
17
|
+
#### Alarm.swift
|
13
|
-
|
14
18
|
|
15
19
|
```Swift
|
16
20
|
|
17
|
-
import
|
21
|
+
import UIKit
|
22
|
+
|
18
|
-
|
23
|
+
import AVFoundation
|
24
|
+
|
25
|
+
|
26
|
+
|
19
|
-
|
27
|
+
class Alarm{
|
20
|
-
|
28
|
+
|
21
|
-
s
|
29
|
+
var selectedWakeUpTime:Date?
|
30
|
+
|
22
|
-
|
31
|
+
var audioPlayer: AVAudioPlayer!
|
32
|
+
|
23
|
-
var
|
33
|
+
var sleepTimer: Timer?
|
34
|
+
|
24
|
-
|
35
|
+
var seconds = 0
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
func runTimer(){
|
40
|
+
|
41
|
+
seconds = calculateInterval(userAwakeTime: selectedWakeUpTime!)
|
42
|
+
|
25
|
-
Te
|
43
|
+
guard sleepTimer == nil else{return}
|
26
|
-
|
44
|
+
|
27
|
-
|
45
|
+
if sleepTimer == nil{
|
46
|
+
|
28
|
-
|
47
|
+
sleepTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
|
48
|
+
|
29
|
-
}
|
49
|
+
}
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
@objc private func updateTimer(){
|
56
|
+
|
57
|
+
if seconds != 0{
|
58
|
+
|
59
|
+
seconds -= 1
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
print(seconds)
|
64
|
+
|
65
|
+
}else{
|
66
|
+
|
67
|
+
sleepTimer?.invalidate()
|
68
|
+
|
69
|
+
sleepTimer = nil
|
70
|
+
|
71
|
+
let soundFilePath = Bundle.main.path(forResource: "", ofType: "")!
|
72
|
+
|
73
|
+
let sound:URL = URL(fileURLWithPath: soundFilePath)
|
74
|
+
|
75
|
+
do{
|
76
|
+
|
77
|
+
audioPlayer = try AVAudioPlayer(contentsOf: sound, fileTypeHint: nil)
|
78
|
+
|
79
|
+
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
|
80
|
+
|
81
|
+
try AVAudioSession.sharedInstance().setActive(true)
|
82
|
+
|
83
|
+
}catch{
|
84
|
+
|
85
|
+
print("Could not load file")
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
audioPlayer.play()
|
90
|
+
|
91
|
+
print(audioPlayer.isPlaying)
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
private func calculateInterval(userAwakeTime:Date)->Int{
|
100
|
+
|
101
|
+
var interval = Int(userAwakeTime.timeIntervalSinceNow)
|
102
|
+
|
103
|
+
if interval < 0{
|
104
|
+
|
105
|
+
interval = 86400 - (0 - interval)
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
let calendar = Calendar.current
|
112
|
+
|
113
|
+
let seconds = calendar.component(.second, from: userAwakeTime)
|
114
|
+
|
115
|
+
return interval - seconds
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
@IBOutlet var sleepTimePicker: UIDatePicker!
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
@IBAction func alarmBtnWasPressed(_ sender: UIButton){
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
func stopTimer(){
|
134
|
+
|
135
|
+
if sleepTimer != nil{
|
136
|
+
|
137
|
+
sleepTimer?.invalidate()
|
138
|
+
|
139
|
+
sleepTimer = nil
|
140
|
+
|
141
|
+
}else{
|
142
|
+
|
143
|
+
audioPlayer.stop()
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
30
150
|
|
31
151
|
}
|
32
152
|
|
33
|
-
|
153
|
+
```
|
154
|
+
|
155
|
+
|
156
|
+
|
34
|
-
|
157
|
+
#### SleepingViewController.swift
|
158
|
+
|
159
|
+
```Swift
|
160
|
+
|
161
|
+
import Foundation
|
162
|
+
|
163
|
+
import UIKit
|
164
|
+
|
165
|
+
|
166
|
+
|
35
|
-
s
|
167
|
+
class SleepingViewController: UIViewController{
|
168
|
+
|
36
|
-
|
169
|
+
var currentTime = CurrentTime()
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
@IBOutlet var timeLabel: UILabel!
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
override func viewDidLoad(){
|
178
|
+
|
179
|
+
currentTime.delegate = self
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
@IBAction func closeBtnWasPressed(_ sender: UIButton){
|
186
|
+
|
187
|
+
dismiss(animated: true, completion: nil)
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
37
|
-
|
193
|
+
func updateTime(_ time:String){
|
38
|
-
|
194
|
+
|
39
|
-
|
195
|
+
timeLabel.text = time
|
40
196
|
|
41
197
|
}
|
42
198
|
|
@@ -46,7 +202,7 @@
|
|
46
202
|
|
47
203
|
|
48
204
|
|
49
|
-
####
|
205
|
+
#### SetViewController.swift
|
50
206
|
|
51
207
|
```Swift
|
52
208
|
|
@@ -56,125 +212,49 @@
|
|
56
212
|
|
57
213
|
|
58
214
|
|
215
|
+
class SetViewController: UIViewController{
|
216
|
+
|
217
|
+
|
218
|
+
|
59
|
-
|
219
|
+
let alarm = Alarm()
|
60
|
-
|
61
|
-
|
220
|
+
|
62
|
-
|
63
|
-
|
221
|
+
|
64
|
-
|
65
|
-
var sleepTimer: Timer?
|
66
|
-
|
67
|
-
var seconds = 0
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
func runTimer(){
|
72
|
-
|
73
|
-
seconds = calculateInterval(userAwakeTime: selectedWakeUpTime!)
|
74
|
-
|
75
|
-
guard sleepTimer == nil else{return}
|
76
|
-
|
77
|
-
if sleepTimer == nil{
|
78
|
-
|
79
|
-
sleepTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
@objc private func updateTimer(){
|
88
|
-
|
89
|
-
if seconds != 0{
|
90
|
-
|
91
|
-
seconds -= 1
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
print(seconds)
|
96
|
-
|
97
|
-
}else{
|
98
|
-
|
99
|
-
sleepTimer?.invalidate()
|
100
|
-
|
101
|
-
sleepTimer = nil
|
102
|
-
|
103
|
-
let soundFilePath = Bundle.main.path(forResource: "", ofType: "")!
|
104
|
-
|
105
|
-
let sound:URL = URL(fileURLWithPath: soundFilePath)
|
106
|
-
|
107
|
-
do{
|
108
|
-
|
109
|
-
audioPlayer = try AVAudioPlayer(contentsOf: sound, fileTypeHint: nil)
|
110
|
-
|
111
|
-
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
|
112
|
-
|
113
|
-
try AVAudioSession.sharedInstance().setActive(true)
|
114
|
-
|
115
|
-
}catch{
|
116
|
-
|
117
|
-
print("Could not load file")
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
audioPlayer.play()
|
122
|
-
|
123
|
-
print(audioPlayer.isPlaying)
|
124
|
-
|
125
|
-
}
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
private func calculateInterval(userAwakeTime:Date)->Int{
|
132
|
-
|
133
|
-
var interval = Int(userAwakeTime.timeIntervalSinceNow)
|
134
|
-
|
135
|
-
if interval < 0{
|
136
|
-
|
137
|
-
interval = 86400 - (0 - interval)
|
138
|
-
|
139
|
-
}
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
let calendar = Calendar.current
|
144
|
-
|
145
|
-
let seconds = calendar.component(.second, from: userAwakeTime)
|
146
|
-
|
147
|
-
return interval - seconds
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
|
152
222
|
|
153
223
|
@IBOutlet var sleepTimePicker: UIDatePicker!
|
154
224
|
|
225
|
+
|
226
|
+
|
155
|
-
|
227
|
+
override func viewDidLoad(){
|
228
|
+
|
156
|
-
|
229
|
+
super.viewDidLoad()
|
230
|
+
|
231
|
+
sleepTimePicker.datePickerMode = UIDatePicker.Mode.time
|
232
|
+
|
157
|
-
|
233
|
+
sleepTimePicker.setDate(Date(), animated: false)
|
158
|
-
|
159
|
-
|
160
|
-
|
234
|
+
|
161
|
-
}
|
235
|
+
}
|
162
|
-
|
163
|
-
|
164
|
-
|
236
|
+
|
237
|
+
|
238
|
+
|
165
|
-
func
|
239
|
+
override func viewDidAppear(_ animated: Bool){
|
166
|
-
|
240
|
+
|
167
|
-
if sleepTimer != nil{
|
241
|
+
if alarm.sleepTimer != nil{
|
168
|
-
|
169
|
-
|
242
|
+
|
170
|
-
|
171
|
-
sleepTimer = nil
|
172
|
-
|
173
|
-
}else{
|
174
|
-
|
175
|
-
a
|
243
|
+
alarm.stopTimer()
|
176
|
-
|
244
|
+
|
177
|
-
}
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
@IBAction func alatmBtnWasPressed(_ sender: UIButton){
|
252
|
+
|
253
|
+
alarm.selectedWakeUpTime = sleepTimePicker.date
|
254
|
+
|
255
|
+
alarm.runTimer()
|
256
|
+
|
257
|
+
performSegue(withIdentifier: "setToSleeping", sender: nil)
|
178
258
|
|
179
259
|
}
|
180
260
|
|
@@ -186,166 +266,60 @@
|
|
186
266
|
|
187
267
|
|
188
268
|
|
189
|
-
####
|
269
|
+
#### CurrentTime.swift
|
190
270
|
|
191
271
|
```Swift
|
192
272
|
|
193
273
|
import Foundation
|
194
274
|
|
275
|
+
|
276
|
+
|
277
|
+
class CurrentTime{
|
278
|
+
|
195
|
-
im
|
279
|
+
var timer: Timer?
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
280
|
+
|
200
|
-
|
201
|
-
var currentTime
|
281
|
+
var currentTime: String?
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
282
|
+
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
283
|
+
var df = DateFormatter()
|
284
|
+
|
210
|
-
|
285
|
+
weak var delegate: SleepingViewController?
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
init(){
|
290
|
+
|
211
|
-
|
291
|
+
if timer == nil{
|
292
|
+
|
212
|
-
|
293
|
+
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCurrentTime), userInfo: nil, repeats: true)
|
294
|
+
|
213
|
-
}
|
295
|
+
}
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
296
|
+
|
218
|
-
|
219
|
-
dismiss(animated: true, completion: nil)
|
220
|
-
|
221
|
-
}
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
222
|
-
|
301
|
+
@objc private func updateCurrentTime(){
|
302
|
+
|
223
|
-
|
303
|
+
df.dateFormat = "HH:mm"
|
304
|
+
|
224
|
-
|
305
|
+
df.timeZone = TimeZone.current
|
306
|
+
|
307
|
+
let timezoneDate = df.string(from: Date())
|
308
|
+
|
309
|
+
currentTime = timezoneDate
|
310
|
+
|
225
|
-
|
311
|
+
delegate?.updateTime(currentTime!)
|
226
|
-
|
227
|
-
timeLabel.text = time
|
228
312
|
|
229
313
|
}
|
230
314
|
|
231
315
|
}
|
232
316
|
|
317
|
+
|
318
|
+
|
233
319
|
```
|
234
320
|
|
235
321
|
|
236
322
|
|
237
|
-
#### SetViewController.swift
|
238
|
-
|
239
|
-
```Swift
|
240
|
-
|
241
|
-
|
323
|
+
#### ファイル構成
|
242
|
-
|
243
|
-
|
324
|
+
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
class SetViewController: UIViewController{
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
let alarm = Alarm()
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
@IBOutlet var sleepTimePicker: UIDatePicker!
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
override func viewDidLoad(){
|
260
|
-
|
261
|
-
super.viewDidLoad()
|
262
|
-
|
263
|
-
sleepTimePicker.datePickerMode = UIDatePicker.Mode.time
|
264
|
-
|
265
|
-
|
325
|
+
![イメージ説明](197e4a0ff83186272526d01a4ab522f2.png)
|
266
|
-
|
267
|
-
}
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
override func viewDidAppear(_ animated: Bool){
|
272
|
-
|
273
|
-
if alarm.sleepTimer != nil{
|
274
|
-
|
275
|
-
alarm.stopTimer()
|
276
|
-
|
277
|
-
}
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
@IBAction func alatmBtnWasPressed(_ sender: UIButton){
|
284
|
-
|
285
|
-
alarm.selectedWakeUpTime = sleepTimePicker.date
|
286
|
-
|
287
|
-
alarm.runTimer()
|
288
|
-
|
289
|
-
performSegue(withIdentifier: "setToSleeping", sender: nil)
|
290
|
-
|
291
|
-
}
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
}
|
296
|
-
|
297
|
-
```
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
#### CurrentTime.swift
|
302
|
-
|
303
|
-
```Swift
|
304
|
-
|
305
|
-
import Foundation
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
class CurrentTime{
|
310
|
-
|
311
|
-
var timer: Timer?
|
312
|
-
|
313
|
-
var currentTime: String?
|
314
|
-
|
315
|
-
var df = DateFormatter()
|
316
|
-
|
317
|
-
weak var delegate: SleepingViewController?
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
init(){
|
322
|
-
|
323
|
-
if timer == nil{
|
324
|
-
|
325
|
-
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCurrentTime), userInfo: nil, repeats: true)
|
326
|
-
|
327
|
-
}
|
328
|
-
|
329
|
-
}
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
@objc private func updateCurrentTime(){
|
334
|
-
|
335
|
-
df.dateFormat = "HH:mm"
|
336
|
-
|
337
|
-
df.timeZone = TimeZone.current
|
338
|
-
|
339
|
-
let timezoneDate = df.string(from: Date())
|
340
|
-
|
341
|
-
currentTime = timezoneDate
|
342
|
-
|
343
|
-
delegate?.updateTime(currentTime!)
|
344
|
-
|
345
|
-
}
|
346
|
-
|
347
|
-
}
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
```
|