【问题标题】:IOS - How to hide a view by touching anywhere outside of itIOS - 如何通过触摸视图之外的任何位置来隐藏视图
【发布时间】:2017-07-12 03:30:03
【问题描述】:

我是 IOS 编程新手,我在单击按钮时显示一个视图,在按钮方法中使用以下代码。

 @IBAction func moreButton(_ sender: Any)
    {
        self.helpView.isHidden = false
    }

最初,self.helpView.isHidden 在 viewDidLoad 方法中设置为 true 以隐藏视图。现在,我如何通过触摸视图外的任何位置来关闭此视图。从研究中,我发现,可以通过创建一个适合整个 viewController 的透明按钮来完成。因此,然后通过单击按钮,我们可以使视图关闭。谁能给我swift 3中的代码来创建这样的按钮。

或者,如果有任何其他更好的方法来隐藏视图,欢迎使用。

我使用的是 Xcode 8.2,swift 3.0

提前致谢。

【问题讨论】:

  • 您阅读的方式应该很好并且易于实现,您也可以覆盖touchesBegan并检查触摸视图是否为视图/或比较触摸位置以隐藏它跨度>

标签: ios swift uiview


【解决方案1】:

在联系开始时你应该这样写

override func touchesBegan(_ touches: Set<AnyHashable>, withEvent event: UIEvent) {
    var touch: UITouch? = touches.first
    //location is relative to the current view
    // do something with the touched point
    if touch?.view != yourView {
        yourView.isHidden = true
    }
}

【讨论】:

  • 谢谢。通过这样做,helpView 不会通过触摸它而关闭。正如预期的那样。
  • 我可以知道确切的问题
  • 我希望 helpView 仅在我们触摸该视图之外时关闭,而不是通过单击该视图本身。所以,我的意思是这段代码的工作方式与我预期的完全一样。
  • for swift 3: touchesBegan(_ touches: Set, with event: UIEvent?)
  • 如果在您要关闭的视图之外还有另一个视图,这不会起作用吗?
【解决方案2】:

在 Swift 4 中

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     let touch = touches.first
     if touch?.view == self.view {
        commentsTxtView.resignFirstResponder()
    }
}

【讨论】:

    【解决方案3】:

    斯威夫特 5.1:

    一旦在视图之外触摸,这应该有助于关闭视图。

     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
        {
            let touch = touches.first
            if touch?.view != self.yourView
            { self.dismiss(animated: true, completion: nil) }
        }
    

    【讨论】:

    • 嘿@Santhosh。我很感激你的回答。它有帮助!
    • @iHarshil ,很高兴它有帮助。随时!!
    • 另一个版本是touch?.view == defaultView。 defaultView 是全屏 UIView(Safe Area 的父级),当您创建新的UIViewController(使用插座获取访问权限)时,XIB 默认添加。此视图始终位于“背景”中,因此如果您创建例如不会填满整个屏幕的自定义弹出窗口,“外部”始终是默认视图。
    【解决方案4】:

    您可以为此使用touchesBegan 方法:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
        self.helpView.isHidden = true
    }
    

    【讨论】:

      【解决方案5】:

      斯威夫特

       override func touchesBegan(_ touches: Set<AnyHashable>, withEvent event: UIEvent) {
           var touch: UITouch? = touches.first
           if touch?.view != self.yourView {
               self.yourView.isHidden = true
           }
       }
      

      目标 C

       - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  {
           UITouch *touch = [touches anyObject];
           if(touch.view != self.yourView){
               self.yourView.hidden = YES;
           }
       }
      

      【讨论】:

        【解决方案6】:

        在选中的moreButton里面,你可以做这样的事情

         @IBAction func moreButton(_ sender: Any)
            {
                self.helpView.isHidden = false
                let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissView))
                view.addGestureRecognizer(tap)
            }
        
            func dismissView() {
                self.helpView.isHidden = true
                self.view.removeGestureRecognizer(tap)
            }
        

        【讨论】:

        • 感谢您的回复。通过添加这个,我得到两个错误。 “使用未解析的标识符‘tap’”和“类型‘UIViewController’没有成员‘dismissView’”。我确定我已经添加了您建议的所有代码。我还有什么需要做的吗?
        • 嗨。尝试输入 view.view。我认为你已经分配了 view = ViewController。
        • 还要在函数外声明“tap”。 let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissView)) 然后把@IBAction func moreButton(_ sender: Any) { self.helpView.isHidden = false view.addGestureRecognizer(tap) }
        【解决方案7】:

        您可以创建另一个透明按钮,或者,您的基本视图(假设它是按钮下方的单个视图)然后可以解决您要执行的操作。首先,您需要使其可点击。然后你想处理点击或未点击时发生的逻辑。

        【讨论】:

          【解决方案8】:

          您可以通过以下方式实现您想要的(使用 Swift 4.1 和 Xcode 9.3 测试):

          class ViewController: UIViewController {
          
              ...
          
              private var dismissViewTap: UITapGestureRecognizer?
          
              override func viewDidLoad() {
          
                  super.viewDidLoad()
          
                  dismissViewTap = UITapGestureRecognizer(target: self, action: #selector(dismissView))
          
                  if let tap = dismissViewTap {
          
                      view.addGestureRecognizer(tap)
          
                  } // if let
          
              } // viewDidLoad
          
              @objc private func dismissView() {
          
                  guard let tap = dismissViewTap else {
          
                      return
          
                  } // guard
          
                  guard helpView.isHidden == false else {
          
                      return
          
                  } // guard
          
                  helpView.isHidden = true
          
                  view.removeGestureRecognizer(tap)
          
              } // dismissView
          
              ...
          
          } // ViewController
          

          如果您想保留手势识别器(可能是因为您多次打开 helpView),请将dismissView 更改为此版本:

          ...
          
          @objc private func dismissView() {
          
              guard helpView.isHidden == false else {
          
                  return
          
              } // guard
          
              helpView.isHidden = true
          
          } // dismissView
          
          ...
          

          就是这样……!

          【讨论】:

            【解决方案9】:

            你可以在swift 4中使用这个方法。

            将标签编号添加到您要添加操作的uiview

            override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
            {
                if touches.first?.view?.tag == 10{
                    dismiss(animated: true, completion: nil)
                    super.touchesEnded(touches , with: event)
                }
            }
            

            【讨论】:

              【解决方案10】:

              就我而言,我遵循的是 touchesEnded 逻辑,

              -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
                  UITouch *touch = [touches anyObject];
                  if([touch view]== self.UIViewThatNeedsToBeDismissed) {
                      NSLog(@"inside view touch");
                  } else {
                      NSLog(@"release it");
                  }
              }
              

              【讨论】:

                【解决方案11】:

                您可以使用以下功能,对视图名称的某些检查将不起作用,但如果您有多个视图,则会出现问题,如果您在第二个视图中点击,则会执行相同的操作。我更愿意添加 TapGesture。这是另一种解决方案。

                override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
                
                
                }
                

                【讨论】:

                  【解决方案12】:
                  private func addClickToDismiss() {
                  
                      let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissPresentedView(_:)))
                      tapRecognizer.cancelsTouchesInView = false
                      presentedViewController.view.superview?.isUserInteractionEnabled = true
                      presentedViewController.view.superview?.addGestureRecognizer(tapRecognizer)
                  }
                  
                  @objc
                  private func dismissPresentedView(_ sender: Any?) {
                      presentedViewController.dismiss(animated: true)
                  }
                  

                  如果有人想要一些复制意大利面,这就是你从自定义 UIPresentationController 中做到的方式

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2014-01-23
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-03-13
                    • 1970-01-01
                    • 2011-04-19
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多