問題
ビューに指の動きを表示する機能(シミュレータでいう●印)を実装しております。
UIViewの派生クラスを用意し、ツールバーやナビゲーションバーの上に被せるように追加しています。
self.navigationController?.view.addSubview(PresenView)
参考:https://qiita.com/akatsuki174/items/0681bd314b04fd64eb18
そこで、UIViewControllerの派生クラスとUIViewの派生クラスの両方のイベント(タッチやボタンなど)を拾いたいのですが、良い方法はありますでしょうか。
現在は、UIViewの派生クラスのtouchイベントしか通知されない状況です。
良いやり方をご存じの方がいらっしゃいましたら、ご教示のほどお願い申し上げます。
調べたこと
①hitTest()をオーバーライドすると、指定したどちらか一方のビューへ通知することができるようです。しかし、両方に通知することは難しそうでした。
②UIViewControllerの派生クラスの方のtouchesBegan()などで、PresenView.touchesBegan(touches, with: event)のようにUIViewの派生クラスのタッチイベントを呼び出すことはできそうでした。しかし、今回ナビゲーションバーやタブバーを利用しており、その場合はtouchesBegan()などに来てくれないため、悩んでいます。
該当のソースコード
swift
1class ViewController: UIViewController{ 2 override func viewDidLoad() { 3 super.viewDidLoad() 4 5 // ビューの追加 6 PresenView = PrezenforView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)) 7 PresenView.backgroundColor = UIColor.clear 8 PresenView.isHidden = false 9 PresenView.isUserInteractionEnabled = true 10 PresenView.isMultipleTouchEnabled = true // マルチタッチ可 11 self.navigationController?.view.addSubview(PresenView) 12 } 13 14 // ----- タッチ関連 ----- 15 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 16 17 // 来ない 18 } 19 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 20 21 // 来ない 22 } 23 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 24 25 // 来ない 26 } 27 28 // 略 29} 30 31// カスタムビュー 32class PrezenforView: UIView { 33 34 var ptarray:Array<CGPoint> = [] 35 36 override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 37 let view = super.hitTest(point, with: event) 38 39 //if(view == self){ 40 // ここを入れるとPrezenforViewのタッチイベントが発生しない 41 // return nil 42 //} 43 return view 44 } 45 46 // ----- タッチ関連 ----- 47 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 48 49 var arPt:Array<CGPoint> = [] 50 51 for touch in touches { 52 let location = touch.location(in: self) 53 arPt.append(location) 54 } 55 56 Setptarray(array: arPt) 57 } 58 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 59 60 var arPt:Array<CGPoint> = [] 61 62 for touch in touches { 63 let location = touch.location(in: self) 64 arPt.append(location) 65 } 66 67 Setptarray(array: arPt) 68 } 69 70 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 71 72 Removeptarray() 73 } 74 75 override func draw(_ rect: CGRect) { 76 77 for i in 0..<ptarray.count { 78 // 円の描画 79 let circle = UIBezierPath(arcCenter: CGPoint(x: ptarray[i].x, y: ptarray[i].y), radius:18, startAngle: CGFloat(Double.pi*2.0*0.0/360.0), endAngle:CGFloat(Double.pi*2.0*360.0/360.0), clockwise: true) 80 let circleColor = UIColor(red:0, green:0, blue:1, alpha:0.6) 81 circleColor.setStroke() // 線の色を設定 82 circleColor.setFill() // 内側塗りつぶし色の設定 83 circle.fill() // 内側塗りつぶし 84 circle.lineWidth = 0 85 circle.stroke() 86 } 87 } 88 89 // 座標配列の設定 90 func Setptarray(array: Array<CGPoint>){ 91 ptarray = array 92 93 self.setNeedsDisplay() // 描画更新 94 } 95 96 // 座標配列の削除 97 func Removeptarray(){ 98 ptarray.removeAll() 99 100 self.setNeedsDisplay() // 描画更新 101 } 102 103}
補足情報
Xcode:Version 10.2
Swift5
iOS12.2

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/12 02:49
2019/06/12 05:12
2019/06/12 05:16
2019/06/12 07:02
2019/06/12 07:11
2019/06/12 07:22
2019/06/12 07:26
2019/06/12 07:32
2019/06/12 07:36