UIView的touchesBegan方法没有被调用

3

我已经创建了一个UIView的子类,并通过引用xib文件添加了视图。

我重写了touchesBegantouchesEnd函数,以便在用户按下此视图时具有自定义效果的“颜色变化”。

以下是这个子类:

class CustomUIView: UIView {

    @IBOutlet var contentView: UIView!

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

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

    private func commonInit () {
        Bundle.main.loadNibNamed("CustomUIView", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touch begin")
        super.touchesBegan(touches, with: event)
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touch ended")
        super.touchesEnded(touches, with: event)
    }

}

然而,触摸事件的 touchesBegantouchesEnd 从未被调用。

我已经在Google和StackOverflow上搜索了3个小时,但仍然找不到任何有效的解决方案...

以下是我尝试过的:

  1. 确保所有的父视图都启用了用户交互。我甚至检查了视图层次结构。
  2. 添加了一个 touchUpInside 的 IBAction 看看是否可以触发该 IBAction。答案是肯定的。
  3. 尝试子类化 UIControl 并重写 beginTracking,但它也没有被触发。

以下是我可以从此自定义视图的 ViewHierarchy 中看到的属性:

  1. 已启用
  2. 用户交互已启用
  3. 多点触控关闭
  4. 透明度为 1
  5. 背景 <nil color> (我还尝试将其设置为具有颜色。但并没有起作用。)
  6. 不透明开启
  7. 隐藏关闭
  8. 清除图形上下文开启
  9. 剪辑边界关闭
  10. 自动调整子视图开启

非常感谢您的帮助!


contentView是什么? - mugx
xib文件的根视图。但是正如你所看到的,我还没有使用它。 - Sira Lam
2
根据您的代码,我怀疑所有发送到自定义视图的触摸事件都在“contentView”上接收到。尝试禁用“contentView”的用户交互。 - Fahri Azimov
请访问并阅读讨论栏目 https://developer.apple.com/documentation/uikit/uiresponder/1621142-touchesbegan - Ashish Sharma
我用这段代码做了一个测试项目,touches正常工作。附上了从storyboard中提取的CustomUIView,并注释了contentView。你可以自己试试看。 - mugx
显示剩余4条评论
2个回答

3
感谢@Fahri Azimov的评论,原因是contentView启用了用户交互功能。因此,在将触摸事件传递给CustomUIView之前,它消耗了所有触摸事件。我已经点赞他的评论。
教训是,xib文件的根视图不是CustomUIView本身,而是它的一个子视图。

0

请尝试一下这个,这是由于您的CustomView的框架问题。

class CustomUIView: UIView {

    var contentView: UIView!
    override init (frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init? (coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    func loadFromNib() -> UIView {
                let bundle = Bundle(for: type(of: self))
                let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
                let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
                return view
        }
    private func commonInit () {
        contentView = self.loadFromNib()
        addSubview(contentView)
        contentView.frame = self.bounds
        self .isUserInteractionEnabled = true
        contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touch begin")
        super.touchesBegan(touches, with: event)
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touch ended")
        super.touchesEnded(touches, with: event)
    }

}

我可以在控制台看到日志

enter image description here

在你的ViewController中拖动一个UIView,并将你的customview类设置为CustomUIView。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接