检测源自 superview 的触摸

Detect touch that originated from superview

我正在创建 UIView 的子类以在项目中使用,并将处理主视图上的触摸。我希望当触摸(在主视图上)被拖动并接触特殊的 UIViews 时,它们会改变它们的背景颜色。使用 UIView 的默认 "touchesMoved" 函数仅检测源自特定视图的触摸。我可以设置主视图来检查自定义 UIView 的每个实例,但这将违反封装并导致混乱的代码。

有什么想法吗?

我不确定这是你想要的,但是如果我把下面的代码放在我的视图控制器中,它的视图中有几个 RDView 视图,当我从RDView 的主视图。

    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        var touched = self.view.hitTest((touches.anyObject() as UITouch).locationInView(self.view), withEvent: event)
        if touched is RDView {
            touched?.backgroundColor = UIColor.redColor()
        }
    }