【问题标题】:Remove a view when tapped outside its frame在框架外点击时移除视图
【发布时间】:2013-12-24 12:32:30
【问题描述】:

我正在使用复杂的视图层次结构,我希望当其中一个子视图在其边界之外被点击时,它应该被删除。我曾尝试使用带有点击手势的透明视图,但它不允许我使用上述子视图。

我该如何实现?

【问题讨论】:

    标签: ios iphone ipad uiview frame


    【解决方案1】:

    创建如下层次结构(为此创建 XIB):

    1) 创建一个全屏大小的透明容器视图并保留对它的引用

    2) 添加一个全屏大小的透明UIButton并设置其动作方法

    3) 在全屏透明按钮上添加子视图

    现在,只要你想显示 subView,就显示这个容器视图。 每当用户在子视图之外点击时,您的按钮点击操作方法将被调用。在这种方法中,您可以使用 removeFromSuperView 删除整个 Container 视图

    【讨论】:

    • 我使用了轻击手势而不是按钮,并使用轻击点来比较它是否位于特定视图中。
    【解决方案2】:
    [yourView removeFromSuperview]; /// if you want to show that view again you need to allocate it again.  
      (or)
    
    [yourView setHidden:YES]; /// if you want to show that view again Just set it as NO.  
    

    【讨论】:

      【解决方案3】:

      我将在子控制器中使用委托,例如

      @protocol ViewControllerDelegate <NSObject>
      
      -(void)dismissPopupViewController;
      
      
      @end
      

      并将其设置为带子控制器的父控制器,此方法将关闭子控制器。

      【讨论】:

        【解决方案4】:

        请使用 UITouch 委托方法:

        – touchesBegan:withEvent:

        – touchesMoved:withEvent:

        – touchesEnded:withEvent:

        – touchesCancelled:withEvent:

        获取子视图的位置并进行相应管理。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-24
          • 1970-01-01
          • 2019-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-04
          相关资源
          最近更新 更多