2017-01-29 5 views
0

私はUITapGestureRecogniserを扱うビューAを持っています。それが自分の上にあるときは、すべてがうまくいく。サブビュー時にTapGestureRecogniserが呼び出されない

私は、View Aオブジェクトの6つを保持する別のView Bを持っています。 ViewControllerにView Bを追加すると、UITapGestureRecogniserは機能しなくなります。

私はすべてのビューでisUserInteractionEnabled = trueを持っています。

誰もがなぜ機能しないのかを知ることができますか?

タッチ時にtapGestureが有効になっているかどうかを確認するにはどうすればよいですか?

おかげ

注:のViewControllerがそれにどんなUIGestureRecognisersを持っていない


SingleView

class QTSingleSelectionView: UIView { 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     initialize() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     initialize() 
    } 

    fileprivate func initialize() { 
     let tap = UITapGestureRecognizer(target: self, action: #selector(onTapListener)) 
     addGestureRecognizer(tap) 
    } 

    func onTapListener() { 
     print("tap") 
     _isSelected.toggle() 
     setSelection(isSelected: _isSelected, animated: true) 
    } 

} 

複数のビュー

class QTMutipleChoiceQuestionSelector: UIView { 

    var selectors: [QTSingleSelectionView] = [] 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     initialize() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     initialize() 
    } 

    fileprivate func initialize() { 

     for selector in selectors { 
      selector.removeFromSuperview() 
     } 

     selectors.removeAll() 

     guard let dataSource = dataSource else { return } 

     let count = dataSource.numberOfAnswers(self) 

     for index in 0..<count { 
      let selector = QTSingleSelectionView() 

      selector.frame = CGRect(x: 0, y: topMargin + heightOfSelector*CGFloat(index), width: frame.width, height: heightOfSelector) 
      selector.text = dataSource.mutipleChoiceQuestionSelector(self, textForAnswerAtIndex: index) 
      selector.selected = dataSource.mutipleChoiceQuestionSelector(self, isItemSelectedAtIndex: index) 
      selector.isUserInteractionEnabled = true 
      addSubview(selector) 
     } 
    } 

} 

ViewContro充填剤

lazy var multipleChoiceView: QTMutipleChoiceQuestionSelector = { 
    let selector = QTMutipleChoiceQuestionSelector() 
    selector.dataSource = self 
    selector.isUserInteractionEnabled = true 
    return selector 
}() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    view.addSubview(multipleChoiceView) 

} 
+0

? –

+0

@AndréSlotta私はコードを追加しました。ありがとう – ilan

+0

init()の代わりにinit(フレーム:CGRect)イニシャライザを呼び出さないために問題が発生しましたが、私の答えに反対しました。ビュー '、私はまた、私のコメントでは、あなたのマルチビューを確認するためにそれがジェスチャーレシーバーを受信したが、私の答えを投票ダウン! –

答えて

1

私はあなたのコードをテストしました。 問題がラインである:

let selector = QTMutipleChoiceQuestionSelector() //Wrong! 

この行は、それはあなたがそれを見ることができた場合でも、任意のタッチイベントを受け取ることができないことを意味し、フレーム(0,0,0,0)でビューを初期化します。 ビューイベントを受け取るために、サイズを与えているソリューション:あなたはいくつかのコードを示すことができ

let selector = QTMutipleChoiceQuestionSelector(frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height)) 
0

はこれを試してみてください - の代わりに:

for index in 0..<count { 
     let selector = QTSingleSelectionView() 

     selector.frame = CGRect(x: 0, y: topMargin + heightOfSelector*CGFloat(index), width: frame.width, height: heightOfSelector) 
     selector.text = dataSource.mutipleChoiceQuestionSelector(self, textForAnswerAtIndex: index) 
     selector.selected = dataSource.mutipleChoiceQuestionSelector(self, isItemSelectedAtIndex: index) 
     selector.isUserInteractionEnabled = true 
     addSubview(selector) 
    } 

あなたが使用する必要があります。

for index in 0..<count { 
    let aFrame: CGRect = CGRect(x: 0, y: topMargin + heightOfSelector*CGFloat(index), width: frame.width, height: heightOfSelector) 
    let selector = QTSingleSelectionView(frame: aFrame) 

    selector.text = dataSource.mutipleChoiceQuestionSelector(self, textForAnswerAtIndex: index) 
    selector.selected = dataSource.mutipleChoiceQuestionSelector(self, isItemSelectedAtIndex: index) 
    selector.isUserInteractionEnabled = true 
    addSubview(selector) 
} 

これは、内部initializer()を持ってinit(frame: CGRect)ある適切な初期化子を呼び出します。しかし、今すぐあなたのコードのデフォルトのinit()UIViewの機能はあなたのカスタムを初期化していないの呼び出しを呼び出します。

+0

これはどのように私の質問に答えますか?私はビューの場所に問題はありません、私はtapGestureに問題があります。 – ilan

+0

私は答えを編集し、また下部の説明を追加して、あなたの問題が解決するかどうかを確認してください。 –

+0

initialiserを変更する必要はなく、initialzer関数が呼び出されます。 – ilan

関連する問題