子视图时不会调用TapGestureRecogniser

时间:2017-01-29 10:04:09

标签: ios swift uitapgesturerecognizer

我有一个处理UITapGestureRecogniser的视图A.如果它本身一切都很好。

我有另一个View B,它包含六个View A对象。当我将View B添加到我的ViewController时,UITapGestureRecogniser停止工作。

我对所有观点都有isUserInteractionEnabled = true

有人能说出它为什么不起作用吗?

如何在触摸时检查tapGestures是否被激活?

由于

注意: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)
        }
    }

}

的ViewController

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)

}

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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)
}

这将调用适当的初始值设定项init(frame: CGRect),其中包含initializer()。但是现在您的代码调用init()的默认UIView函数没有自定义初始化。