【问题标题】:Detect touch that originated from superview检测源自 superview 的触摸
【发布时间】:2015-04-08 22:49:03
【问题描述】:

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

有什么想法吗?

【问题讨论】:

    标签: ios swift uiview


    【解决方案1】:

    我不确定这是你的追求,但如果我将以下代码放在我的视图控制器中,它的视图中有几个 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()
            }
        }
    

    【讨论】:

    • 效果很好,谢谢!我不知道 hitTest 方法。现在我要做的就是从 if 语句中调用自定义视图的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多