【问题标题】:How to track clicks outside the custom view如何跟踪自定义视图之外的点击
【发布时间】:2015-12-03 10:21:45
【问题描述】:

我有一个自定义视图,看起来如此

如何跟踪对空白区域(视图外)的点击并将其隐藏?

【问题讨论】:

    标签: ios xcode swift custom-view


    【解决方案1】:

    你可以像这样使用touchesBegan 来跟踪它:

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch= [touches anyObject];
        if ([touch view] == self.view)
        {
            // do stuff
        }
    }
    

    对于快速:

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            if let touch = touches.first {
                if touch.view == self.view {
                    // do stuff
                }
            }
            super.touchesBegan(touches, withEvent:event)
        }
    

    【讨论】:

      【解决方案2】:

      您可以在覆盖整个屏幕的自定义视图下添加UIView,将其设置为 0.1 左右的 alpha。然后,您可以向其添加 tapGestureRecognizer 以捕获自定义视图之外的所有触摸。

      请记住在隐藏自定义视图时也隐藏叠加层,以免之后的触摸被阻止。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多