質問編集履歴

2

ソースコードの修正

2019/12/06 10:35

投稿

entaro12345
entaro12345

スコア75

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,12 @@
36
36
 
37
37
 
38
38
 
39
+ お力添えをいただきタップイベントは取得できましたので、ソースコードを修正しました。
40
+
41
+  →タップしたものの判別処理で悩んでいます。
42
+
43
+
44
+
39
45
  ### 発生している問題・エラーメッセージ
40
46
 
41
47
 
@@ -52,49 +58,219 @@
52
58
 
53
59
 
54
60
 
55
- ```CustomScrollView
61
+ ```ViewController
62
+
56
-
63
+ import UIKit
64
+
65
+
66
+
57
- class CustomScrollView: UIScrollView {
67
+ class ViewController: UIViewController {
68
+
69
+
70
+
58
-
71
+ var activityIndicator: UIActivityIndicatorView!
72
+
73
+ var pageMenu : CAPSPageMenu?
74
+
75
+ var dispatch = DispatchGroup()
76
+
77
+
78
+
79
+ var controllerArray : [UIViewController] = []
80
+
81
+ var viewCtl : SubViewController!
82
+
83
+
84
+
85
+ var iWidthScreen:CGFloat = 0.0
86
+
87
+ var iHeightScreen:CGFloat = 0.0
88
+
89
+
90
+
91
+ override func viewDidLoad() {
92
+
93
+ super.viewDidLoad()
94
+
95
+
96
+
97
+ // プログレスバー表示
98
+
99
+ activityIndicator = UIActivityIndicatorView()
100
+
101
+ activityIndicator.color = UIColor.gray
102
+
103
+ activityIndicator.transform = CGAffineTransform(scaleX: 3, y: 3)
104
+
59
- override func touchEnded(_ touche: Set<UITouch>, with event: UIEvent?) {
105
+ activityIndicator.frame = CGRect(x: 0, y: self.view.frame.height * 0.7, width: self.view.frame.width, height: 100)
106
+
60
-
107
+ activityIndicator.hidesWhenStopped = true
108
+
61
- superview?.touchesEnded(touche, with: event)
109
+ self.view.addSubview(activityIndicator)
110
+
111
+ activityIndicator.startAnimating()
112
+
113
+
114
+
115
+ // 画面サイズを取得する
116
+
117
+ iWidthScreen = self.view.frame.size.width
118
+
119
+ iHeightScreen = self.view.frame.size.height
120
+
121
+
122
+
123
+ // タイトル
124
+
125
+ let lblTitleFacility = UILabel()
126
+
127
+ lblTitleFacility.frame = CGRect(x: 0, y: 45, width: iWidthScreen - 10, height: 5)
128
+
129
+ lblTitleFacility.text = "タイトル"
130
+
131
+ lblTitleFacility.textColor = UIColor.white
132
+
133
+ lblTitleFacility.font = UIFont.systemFont(ofSize: 16)
134
+
135
+ lblTitleFacility.textAlignment = NSTextAlignment.center
136
+
137
+ self.view.addSubview(lblTitleFacility)
62
138
 
63
139
  }
64
140
 
141
+
142
+
143
+ override func viewDidAppear(_ animated: Bool) {
144
+
145
+ // APIから情報を取得
146
+
147
+ let sParam = 省略
148
+
149
+ let sData = sParam.data(using: String.Encoding.utf8)
150
+
151
+
152
+
153
+ // info.plistファイルに設定しAPIのURLを表示
154
+
155
+ let urlString = 省略
156
+
157
+
158
+
159
+ if let url = URL(string: urlString!) {
160
+
161
+ let req = NSMutableURLRequest(url: url)
162
+
163
+ self.dispatch = DispatchGroup()
164
+
165
+ self.dispatch.enter()
166
+
167
+ req.httpMethod = "POST"
168
+
169
+ req.httpBody = sData
170
+
171
+ let task = URLSession.shared.dataTask(with: req as URLRequest, completionHandler: { (data, resp, err) in
172
+
173
+ do {
174
+
175
+ let json:NSArray = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
176
+
177
+
178
+
179
+ for data in json {
180
+
181
+ // データ取得
182
+
183
+ let aData = data as! Dictionary<String, AnyObject>
184
+
185
+ // タブの追加
186
+
187
+ self.viewCtl = SubViewController()
188
+
189
+ // タブ名をセット
190
+
191
+ self.viewCtl.title = "タブ"
192
+
193
+ // 各種コントロールセット
194
+
195
+ 省略
196
+
197
+ }
198
+
199
+ self.dispatch.leave()
200
+
201
+ }
202
+
203
+ })
204
+
205
+ task.resume()
206
+
207
+ }
208
+
209
+
210
+
211
+ dispatch.notify(queue: .main) {
212
+
213
+ // PageMenuの設定
214
+
215
+ let parameters: [CAPSPageMenuOption] = [
216
+
217
+ .bottomMenuHairlineColor(CustomClass.CustomUIColor().rgba()),
218
+
219
+ .centerMenuItems(true),
220
+
221
+ .menuItemFont(UIFont.systemFont(ofSize: 14)),
222
+
223
+ .menuItemSeparatorWidth(4.3),
224
+
225
+ .menuItemSeparatorPercentageHeight(0.1),
226
+
227
+ .menuItemWidthBasedOnTitleTextWidth(true),
228
+
229
+ .selectedMenuItemLabelColor(CustomClass.CustomUIColor().rgba()),
230
+
231
+ .selectionIndicatorColor(CustomClass.CustomUIColor().rgba()),
232
+
233
+ .unselectedMenuItemLabelColor(UIColor.white)
234
+
235
+ ]
236
+
237
+ // PageMenuのビューのサイズを設定
238
+
239
+ self.pageMenu = CAPSPageMenu(viewControllers: self.controllerArray, frame: CGRect(x:0, y:70, width:self.view.frame.width, height:self.view.frame.height * 0.86), pageMenuOptions: parameters)
240
+
241
+ // PageMenuのビューを親のビューに追加
242
+
243
+ self.view.addSubview(self.pageMenu!.view)
244
+
245
+ // PageMenuのビューをToolbarの後ろへ移動
246
+
247
+ self.view.sendSubviewToBack(self.pageMenu!.view)
248
+
249
+ self.pageMenu!.didMove(toParent: self)
250
+
251
+
252
+
253
+ self.activityIndicator.stopAnimating()
254
+
255
+ }
256
+
257
+ }
258
+
259
+
260
+
65
261
  }
66
262
 
67
263
  ```
68
264
 
69
265
 
70
266
 
71
- ```ViewController
267
+ ```SubViewController
72
268
 
73
269
  import UIKit
74
270
 
75
271
 
76
272
 
77
- class ViewController: UIViewController {
273
+ class SubViewController: UIViewController, UIGestureRecognizerDelegate {
78
-
79
-
80
-
81
- var activityIndicator: UIActivityIndicatorView!
82
-
83
- var pageMenu : CAPSPageMenu?
84
-
85
- var dispatch = DispatchGroup()
86
-
87
-
88
-
89
- var controllerArray : [UIViewController] = []
90
-
91
- var viewCtl : SubViewController!
92
-
93
-
94
-
95
- var iWidthScreen:CGFloat = 0.0
96
-
97
- var iHeightScreen:CGFloat = 0.0
98
274
 
99
275
 
100
276
 
@@ -104,166 +280,158 @@
104
280
 
105
281
 
106
282
 
107
- // プログレスバー表示
108
-
109
- activityIndicator = UIActivityIndicatorView()
110
-
111
- activityIndicator.color = UIColor.gray
112
-
113
- activityIndicator.transform = CGAffineTransform(scaleX: 3, y: 3)
114
-
115
- activityIndicator.frame = CGRect(x: 0, y: self.view.frame.height * 0.7, width: self.view.frame.width, height: 100)
116
-
117
- activityIndicator.hidesWhenStopped = true
118
-
119
- self.view.addSubview(activityIndicator)
120
-
121
- activityIndicator.startAnimating()
122
-
123
-
124
-
125
283
  // 画面サイズを取得する
126
284
 
127
- iWidthScreen = self.view.frame.size.width
128
-
129
- iHeightScreen = self.view.frame.size.height
130
-
131
-
132
-
133
- // タイトル
134
-
135
- let lblTitleFacility = UILabel()
136
-
137
- lblTitleFacility.frame = CGRect(x: 0, y: 45, width: iWidthScreen - 10, height: 5)
138
-
139
- lblTitleFacility.text = "タイトル"
140
-
141
- lblTitleFacility.textColor = UIColor.white
142
-
143
- lblTitleFacility.font = UIFont.systemFont(ofSize: 16)
144
-
145
- lblTitleFacility.textAlignment = NSTextAlignment.center
146
-
147
- self.view.addSubview(lblTitleFacility)
285
+ let iWidthScreen:CGFloat = self.view.frame.size.width
286
+
287
+ let iHeightScreen:CGFloat = self.view.frame.size.height
288
+
289
+
290
+
291
+ // タップのンスタンス生成
292
+
293
+ let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapped(_:)))
294
+
295
+ tapGesture.delegate = self
296
+
297
+ self.view.addGestureRecognizer(tapGesture)
298
+
299
+
300
+
301
+ let scrollView = CustomScrollView()
302
+
303
+ scrollView.frame = CGRect(x: 0, y: 15, width: iWidthScreen, height: iHeightScreen * 0.8)
304
+
305
+ scrollView.contentSize = CGSize(width: iWidthScreen, height: 750) // 80 * n + 30
306
+
307
+ scrollView.isUserInteractionEnabled = true
308
+
309
+
310
+
311
+ // 1セル分のコントロールを生成する
312
+
313
+ let view = UIView()
314
+
315
+ view.frame = CGRect(x: 5, y: Int(80 * iNum - 60), width: Int(iWidthScreen - 10), height: 65)
316
+
317
+ view.layer.borderWidth = 1
318
+
319
+ view.layer.borderColor = UIColor.gray.cgColor
320
+
321
+ view.isUserInteractionEnabled = true
322
+
323
+
324
+
325
+ // サイズを取得する
326
+
327
+ let iWidthVScreen:CGFloat = view.frame.size.width
328
+
329
+ let iHeightVScreen:CGFloat = view.frame.size.height
330
+
331
+
332
+
333
+ // 各コントロールを配置
334
+
335
+ let sw = UISwitch()
336
+
337
+ sw.frame = CGRect(x: 5, y: Int(iHeightVScreen * 0.3), width: Int(iWidthVScreen * 0.2), height: 5)
338
+
339
+ sw.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
340
+
341
+ view.addSubview(sw)
342
+
343
+
344
+
345
+ // 名前
346
+
347
+ let lblName = UILabel()
348
+
349
+ lblName.frame = CGRect(x: iWidthVScreen * 0.15, y: iHeightVScreen * 0.3, width: iWidthVScreen * 0.3, height: iHeightVScreen * 0.4)
350
+
351
+ lblName.text = "名前"
352
+
353
+ lblName.textColor = UIColor.white
354
+
355
+ lblName.font = UIFont.boldSystemFont(ofSize: 22)
356
+
357
+ view.addSubview(lblName)
358
+
359
+
360
+
361
+ // 年齢
362
+
363
+ let lblAge = UILabel()
364
+
365
+ lblAge.frame = CGRect(x: iWidthVScreen * 0.15, y: iHeightVScreen * 0.3, width: iWidthVScreen * 0.3, height: iHeightVScreen * 0.4)
366
+
367
+ lblAge.text = "20歳"
368
+
369
+ lblAge.textColor = UIColor.white
370
+
371
+ lblAge.font = UIFont.boldSystemFont(ofSize: 22)
372
+
373
+ view.addSubview(lblAge)
374
+
375
+
376
+
377
+ // 画像1
378
+
379
+ let imgone = UIImage(named: image1)
380
+
381
+ let imgViewone = UIImageView(image: imgone)
382
+
383
+ imgViewone.frame = CGRect(x: iWidthVScreen * 0.8, y: iHeightVScreen * 0.3, width: 30, height: 30)
384
+
385
+ imgViewone.tag = 100
386
+
387
+ view.addSubview(imgViewone)
388
+
389
+
390
+
391
+ // 画像2
392
+
393
+ let imgtwo = UIImage(named: image2)
394
+
395
+ let imgViewtwo = UIImageView(image: imgtwo)
396
+
397
+ imgViewtwo.frame = CGRect(x: iWidthVScreen * 0.8, y: iHeightVScreen * 0.3, width: 30, height: 30)
398
+
399
+ imgViewtwo.tag = 200
400
+
401
+ view.addSubview(imgViewtwo)
402
+
403
+
404
+
405
+ // 画像3
406
+
407
+ let imgthree = UIImage(named: image3)
408
+
409
+ let imgViewthree = UIImageView(image: imgthree)
410
+
411
+ imgViewthree.frame = CGRect(x: iWidthVScreen * 0.8, y: iHeightVScreen * 0.3, width: 30, height: 30)
412
+
413
+ imgViewthree.tag = 300
414
+
415
+ view.addSubview(imgViewthree)
416
+
417
+
418
+
419
+ scrollView.addSubview(view)
420
+
421
+ self.view.addSubview(scrollView)
148
422
 
149
423
  }
150
424
 
151
425
 
152
426
 
153
- override func viewDidAppear(_ animated: Bool) {
427
+ @objc func tapped(_ sender: UITapGestureRecognizer) {
154
-
155
- // APIから情報を取得
428
+
156
-
157
- let sParam = 省略
158
-
159
- let sData = sParam.data(using: String.Encoding.utf8)
160
-
161
-
162
-
163
- // info.plistファイルに設定しAPIのURLを表示
164
-
165
- let urlString = 省略
166
-
167
-
168
-
169
- if let url = URL(string: urlString!) {
170
-
171
- let req = NSMutableURLRequest(url: url)
172
-
173
- self.dispatch = DispatchGroup()
174
-
175
- self.dispatch.enter()
429
+ if sender.state == .ended {
176
-
430
+
177
- req.httpMethod = "POST"
431
+ print("タップしました!")
178
-
179
- req.httpBody = sData
180
-
181
- let task = URLSession.shared.dataTask(with: req as URLRequest, completionHandler: { (data, resp, err) in
182
-
183
- do {
184
-
185
- let json:NSArray = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
186
-
187
-
188
-
189
- for data in json {
190
-
191
- // データ取得
192
-
193
- let aData = data as! Dictionary<String, AnyObject>
194
-
195
- // タブの追加
196
-
197
- self.viewCtl = SubViewController()
198
-
199
- // タブ名をセット
200
-
201
- self.viewCtl.title = "タブ"
202
-
203
- // 各種コントロールセット
204
-
205
- 省略
206
-
207
- }
208
-
209
- self.dispatch.leave()
210
-
211
- }
212
-
213
- })
214
-
215
- task.resume()
216
432
 
217
433
  }
218
434
 
219
-
220
-
221
- dispatch.notify(queue: .main) {
222
-
223
- // PageMenuの設定
224
-
225
- let parameters: [CAPSPageMenuOption] = [
226
-
227
- .bottomMenuHairlineColor(CustomClass.CustomUIColor().rgba()),
228
-
229
- .centerMenuItems(true),
230
-
231
- .menuItemFont(UIFont.systemFont(ofSize: 14)),
232
-
233
- .menuItemSeparatorWidth(4.3),
234
-
235
- .menuItemSeparatorPercentageHeight(0.1),
236
-
237
- .menuItemWidthBasedOnTitleTextWidth(true),
238
-
239
- .selectedMenuItemLabelColor(CustomClass.CustomUIColor().rgba()),
240
-
241
- .selectionIndicatorColor(CustomClass.CustomUIColor().rgba()),
242
-
243
- .unselectedMenuItemLabelColor(UIColor.white)
244
-
245
- ]
246
-
247
- // PageMenuのビューのサイズを設定
248
-
249
- self.pageMenu = CAPSPageMenu(viewControllers: self.controllerArray, frame: CGRect(x:0, y:70, width:self.view.frame.width, height:self.view.frame.height * 0.86), pageMenuOptions: parameters)
250
-
251
- // PageMenuのビューを親のビューに追加
252
-
253
- self.view.addSubview(self.pageMenu!.view)
254
-
255
- // PageMenuのビューをToolbarの後ろへ移動
256
-
257
- self.view.sendSubviewToBack(self.pageMenu!.view)
258
-
259
- self.pageMenu!.didMove(toParent: self)
260
-
261
-
262
-
263
- self.activityIndicator.stopAnimating()
264
-
265
- }
266
-
267
435
  }
268
436
 
269
437
 
@@ -274,188 +442,6 @@
274
442
 
275
443
 
276
444
 
277
- ```SubViewController
278
-
279
- import UIKit
280
-
281
-
282
-
283
- class SubViewController: UIViewController {
284
-
285
-
286
-
287
- override func viewDidLoad() {
288
-
289
- super.viewDidLoad()
290
-
291
-
292
-
293
- // 画面サイズを取得する
294
-
295
- let iWidthScreen:CGFloat = self.view.frame.size.width
296
-
297
- let iHeightScreen:CGFloat = self.view.frame.size.height
298
-
299
-
300
-
301
- let scrollView = CustomScrollView()
302
-
303
- scrollView.frame = CGRect(x: 0, y: 15, width: iWidthScreen, height: iHeightScreen * 0.8)
304
-
305
- scrollView.contentSize = CGSize(width: iWidthScreen, height: 750) // 80 * n + 30
306
-
307
- scrollView.isUserInteractionEnabled = true
308
-
309
-
310
-
311
- // 1セル分のコントロールを生成する
312
-
313
- let view = UIView()
314
-
315
- view.frame = CGRect(x: 5, y: Int(80 * iNum - 60), width: Int(iWidthScreen - 10), height: 65)
316
-
317
- view.layer.borderWidth = 1
318
-
319
- view.layer.borderColor = UIColor.gray.cgColor
320
-
321
- view.isUserInteractionEnabled = true
322
-
323
-
324
-
325
- // サイズを取得する
326
-
327
- let iWidthVScreen:CGFloat = view.frame.size.width
328
-
329
- let iHeightVScreen:CGFloat = view.frame.size.height
330
-
331
-
332
-
333
- // 各コントロールを配置
334
-
335
- let sw = UISwitch()
336
-
337
- sw.frame = CGRect(x: 5, y: Int(iHeightVScreen * 0.3), width: Int(iWidthVScreen * 0.2), height: 5)
338
-
339
- sw.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
340
-
341
- view.addSubview(sw)
342
-
343
-
344
-
345
- // 名前
346
-
347
- let lblName = UILabel()
348
-
349
- lblName.frame = CGRect(x: iWidthVScreen * 0.15, y: iHeightVScreen * 0.3, width: iWidthVScreen * 0.3, height: iHeightVScreen * 0.4)
350
-
351
- lblName.text = "名前"
352
-
353
- lblName.textColor = UIColor.white
354
-
355
- lblName.font = UIFont.boldSystemFont(ofSize: 22)
356
-
357
- view.addSubview(lblName)
358
-
359
-
360
-
361
- // 年齢
362
-
363
- let lblAge = UILabel()
364
-
365
- lblAge.frame = CGRect(x: iWidthVScreen * 0.15, y: iHeightVScreen * 0.3, width: iWidthVScreen * 0.3, height: iHeightVScreen * 0.4)
366
-
367
- lblAge.text = "20歳"
368
-
369
- lblAge.textColor = UIColor.white
370
-
371
- lblAge.font = UIFont.boldSystemFont(ofSize: 22)
372
-
373
- view.addSubview(lblAge)
374
-
375
-
376
-
377
- // 画像1
378
-
379
- let imgone = UIImage(named: image1)
380
-
381
- let imgViewone = UIImageView(image: imgone)
382
-
383
- imgViewone.frame = CGRect(x: iWidthVScreen * 0.8, y: iHeightVScreen * 0.3, width: 30, height: 30)
384
-
385
- imgViewone.tag = 100
386
-
387
- view.addSubview(imgViewone)
388
-
389
-
390
-
391
- // 画像2
392
-
393
- let imgtwo = UIImage(named: image2)
394
-
395
- let imgViewtwo = UIImageView(image: imgtwo)
396
-
397
- imgViewtwo.frame = CGRect(x: iWidthVScreen * 0.8, y: iHeightVScreen * 0.3, width: 30, height: 30)
398
-
399
- imgViewtwo.tag = 200
400
-
401
- view.addSubview(imgViewtwo)
402
-
403
-
404
-
405
- // 画像3
406
-
407
- let imgthree = UIImage(named: image3)
408
-
409
- let imgViewthree = UIImageView(image: imgthree)
410
-
411
- imgViewthree.frame = CGRect(x: iWidthVScreen * 0.8, y: iHeightVScreen * 0.3, width: 30, height: 30)
412
-
413
- imgViewthree.tag = 300
414
-
415
- view.addSubview(imgViewthree)
416
-
417
-
418
-
419
- scrollView.addSubview(view)
420
-
421
- self.view.addSubview(scrollView)
422
-
423
- }
424
-
425
-
426
-
427
- // タップイベント
428
-
429
- override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
430
-
431
- for touch: UITouch in touches {
432
-
433
- let tag = touch.view!.tag
434
-
435
- if (tag == 100 || tag == 200 || tag == 300) {
436
-
437
- if (tag == 100) {
438
-
439
- } else if (tag == 200) {
440
-
441
- } else if (tag == 300) {
442
-
443
- }
444
-
445
- }
446
-
447
- }
448
-
449
- }
450
-
451
-
452
-
453
- }
454
-
455
- ```
456
-
457
-
458
-
459
445
  ### 補足情報(FW/ツールのバージョンなど)
460
446
 
461
447
 

1

修正

2019/12/06 10:35

投稿

entaro12345
entaro12345

スコア75

test CHANGED
File without changes
test CHANGED
@@ -32,6 +32,10 @@
32
32
 
33
33
 
34
34
 
35
+ ※伝わりづらくてすみませんが、お力を貸してください。
36
+
37
+
38
+
35
39
  ### 発生している問題・エラーメッセージ
36
40
 
37
41