質問編集履歴

1

ViewControllerの追加

2019/08/24 06:38

投稿

aaaaa22222
aaaaa22222

スコア9

test CHANGED
File without changes
test CHANGED
@@ -18,4 +18,316 @@
18
18
 
19
19
 
20
20
 
21
+ 助けてください。Controllerのソースは以下です。
22
+
23
+
24
+
25
+ ViewController.swift
26
+
27
+
28
+
21
- 助けてください。
29
+ ```Swift
30
+
31
+ import UIKit
32
+
33
+
34
+
35
+ class ViewController: UIViewController {
36
+
37
+
38
+
39
+ var currentValue = 0
40
+
41
+ var targetValue = 0
42
+
43
+ var score = 0
44
+
45
+ var round = 0
46
+
47
+
48
+
49
+ @IBOutlet weak var slider: UISlider!
50
+
51
+ @IBOutlet weak var targetLabel: UILabel!
52
+
53
+ @IBOutlet weak var scoreLabel: UILabel!
54
+
55
+ @IBOutlet weak var roundLabel: UILabel!
56
+
57
+
58
+
59
+ override func viewDidLoad() {
60
+
61
+ super.viewDidLoad()
62
+
63
+ let roundedValue = slider.value.rounded()
64
+
65
+ currentValue = Int(roundedValue)
66
+
67
+ startNewGame()
68
+
69
+
70
+
71
+ let thumbImageNormal = #imageLiteral(resourceName: "SliderThumb-Normal")
72
+
73
+ slider.setThumbImage(thumbImageNormal, for: .normal)
74
+
75
+
76
+
77
+ let thumbImageHighlighted = #imageLiteral(resourceName: "SliderThumb-Highlighted")
78
+
79
+ slider.setThumbImage(thumbImageHighlighted, for: .highlighted)
80
+
81
+
82
+
83
+ let insets = UIEdgeInsets(top:0, left:14, bottom:0, right:14)
84
+
85
+
86
+
87
+ let trackLeftImage = #imageLiteral(resourceName: "SliderTrackLeft")
88
+
89
+ let trackLeftResizable = trackLeftImage.resizableImage(withCapInsets: insets)
90
+
91
+ slider.setMinimumTrackImage(trackLeftResizable, for: .normal)
92
+
93
+
94
+
95
+ let trackRightImage = #imageLiteral(resourceName: "SliderTrackRight")
96
+
97
+ let trackRightResizable = trackRightImage.resizableImage(withCapInsets: insets)
98
+
99
+ slider.setMaximumTrackImage(trackRightResizable, for: .normal)
100
+
101
+
102
+
103
+ }
104
+
105
+
106
+
107
+ @IBAction func showAlert() {
108
+
109
+
110
+
111
+ let difference = abs(targetValue - currentValue)
112
+
113
+ var points = 100 - difference
114
+
115
+
116
+
117
+ score += points
118
+
119
+
120
+
121
+ let title: String
122
+
123
+ if difference == 0 {
124
+
125
+ title = "Perfect!"
126
+
127
+ points += 100
128
+
129
+ } else if difference < 5 {
130
+
131
+ title = "You almost had it!"
132
+
133
+ if difference == 1 {
134
+
135
+ points += 50
136
+
137
+ }
138
+
139
+ } else if difference < 10 {
140
+
141
+ title = "Pretty good!"
142
+
143
+ } else {
144
+
145
+ title = "Not even close..."
146
+
147
+ }
148
+
149
+
150
+
151
+ let message = "You scored (points) points"
152
+
153
+
154
+
155
+ let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
156
+
157
+
158
+
159
+ let action = UIAlertAction(title: "OK", style: .default, handler: {
160
+
161
+ action in
162
+
163
+ self.startNewRound()
164
+
165
+ })
166
+
167
+
168
+
169
+ alert.addAction(action)
170
+
171
+
172
+
173
+ present(alert, animated: true, completion: nil)
174
+
175
+
176
+
177
+ }
178
+
179
+
180
+
181
+ @IBAction func sliderMoved(_ slider: UISlider) {
182
+
183
+ let roundedValue = slider.value.rounded()
184
+
185
+ currentValue = Int(roundedValue)
186
+
187
+ }
188
+
189
+
190
+
191
+ func startNewRound() {
192
+
193
+ round += 1
194
+
195
+ targetValue = Int.random(in: 1...100)
196
+
197
+ currentValue = 50
198
+
199
+ slider.value = Float(currentValue)
200
+
201
+ updateLabels()
202
+
203
+ }
204
+
205
+
206
+
207
+ func updateLabels() {
208
+
209
+ targetLabel.text = String(targetValue)
210
+
211
+ scoreLabel.text = String(score)
212
+
213
+ roundLabel.text = String(round)
214
+
215
+ }
216
+
217
+
218
+
219
+ @IBAction func startNewGame() {
220
+
221
+ score = 0
222
+
223
+ round = 0
224
+
225
+ startNewRound()
226
+
227
+ }
228
+
229
+
230
+
231
+ }
232
+
233
+
234
+
235
+ ```
236
+
237
+
238
+
239
+ AboutAuthorViewController.swift
240
+
241
+
242
+
243
+ ```Swift
244
+
245
+ import UIKit
246
+
247
+
248
+
249
+ class AboutAuthorViewController: UITableViewController {
250
+
251
+
252
+
253
+ override func viewDidLoad() {
254
+
255
+ super.viewDidLoad()
256
+
257
+ }
258
+
259
+
260
+
261
+ @IBAction func close() {
262
+
263
+ dismiss(animated: true, completion: nil)
264
+
265
+ }
266
+
267
+
268
+
269
+ }
270
+
271
+ ```
272
+
273
+
274
+
275
+ AboutViewController.swift
276
+
277
+
278
+
279
+ ```Swift
280
+
281
+ import UIKit
282
+
283
+ import WebKit
284
+
285
+
286
+
287
+ class AboutViewController: UIViewController {
288
+
289
+
290
+
291
+ @IBOutlet weak var webView: WKWebView!
292
+
293
+
294
+
295
+ override func viewDidLoad() {
296
+
297
+ super.viewDidLoad()
298
+
299
+ if let htmlPath = Bundle.main.path(forResource: "BullsEye", ofType: "html"){
300
+
301
+ let url = URL(fileURLWithPath: htmlPath)
302
+
303
+ let request = URLRequest(url: url)
304
+
305
+ webView.load(request)
306
+
307
+ }
308
+
309
+ }
310
+
311
+
312
+
313
+ @IBAction func close() {
314
+
315
+ dismiss(animated: true, completion: nil)
316
+
317
+ }
318
+
319
+
320
+
321
+ }
322
+
323
+ ```
324
+
325
+
326
+
327
+
328
+
329
+ Main.storyboardのスクショは以下です。
330
+
331
+
332
+
333
+ ![Main.storyboard](e68f12e5d872b74d0e701a9ad640f46a.png)