子视图时未调用 TapGestureRecogniser

TapGestureRecogniser not getting called when a subview

我有一个处理 UITapGestureRecogniser 的视图 A。当它独立时,一切都很好。

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

我对所有观点都有 isUserInteractionEnabled = true

任何人都可以找出它不起作用的原因吗?

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

谢谢

注意:ViewController 上没有任何 UIGestureRecognisers


单视图

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)

}

试试这个 - 而不是:

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()。但是现在您的代码调用的 UIView 的默认 init() 函数没有您的自定义初始化。

我测试了你的代码。 问题在行:

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))