【问题标题】:UIView touchesBegan not being calledUIView touchesBegan 没有被调用
【发布时间】:2018-01-12 05:04:05
【问题描述】:

我创建了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 永远不会被调用。

我用谷歌搜索和 stackoverflowed,已经尝试了 3 个小时,但找不到任何可行的解决方案来解决这个简单的问题....

以下是我尝试过的:

  1. 确保所有超级视图都启用了 UserInteraction。我什至 捕获 View Hierarchy 以检查这一点。
  2. 添加了touchUpInside的IBAction,看看是否可以触发这个IBAction。答案是肯定的。
  3. 改为子类 UIControl 并覆盖 beginTracking。但它也没有被触发。

以下是我可以从这个 CustomView 的 ViewHierarchy 中看到的属性:

  1. 已启用
  2. 启用用户交互
  3. 多次触发关闭
  4. 阿尔法 1
  5. 背景&lt;nil color&gt;(我也尝试将其设置为有颜色。不起作用。)
  6. 不透明开启
  7. 隐藏关闭
  8. 清除图形上下文打开
  9. 剪辑到边界关闭
  10. 自动调整子视图大小

帮助非常感谢!

【问题讨论】:

  • 什么是contentView
  • xib 文件的根视图。但是你可以看到我还没有使用它呢
  • 从您的代码中,我怀疑发送到您的自定义视图的所有触摸事件都是在contentView 上收到的。尝试在 contentView 上禁用用户交互。
  • 我用这样的代码做了一个测试项目,touches 工作正常。从故事板附加CustomUIView,评论contentView。自己试试吧。

标签: ios swift uiview touch


【解决方案1】:

感谢@Fahri Azimov 的评论,原因是contentView 已启用用户交互。因此,它在传递给CustomUIView 之前消耗了所有触摸事件。我赞成他的评论。

教训是,xib 文件的 rootView 不是CustomUIView 本身,而是它的一个孩子。

【讨论】:

    【解决方案2】:

    请尝试一下,这是由于您的 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)
        }
    
    }
    

    我可以在控制台中看到日志

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      相关资源
      最近更新 更多