IOS - How to hide a view by touching anywhere outside of it

后端 未结 12 1516
温柔的废话
温柔的废话 2020-12-29 23:23

I\'m new to IOS programming, I\'m displaying a view when a button is clicked, using the following code inside the button method.

 @IBAction func moreButton(_         


        
相关标签:
12条回答
  • 2020-12-29 23:42

    Inside the moreButton selected, you can do something like this

     @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)
        }
    
    0 讨论(0)
  • 2020-12-29 23:42

    You can achieve what you want doing the following (tested with Swift 4.1 and 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
    

    If you want to keep the gesture recognizer (maybe because you open helpView more than once) change dismissView to this version:

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

    That's all...!

    0 讨论(0)
  • 2020-12-29 23:42

    in my case I followed it by touchesEnded logic,

    -(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");
        }
    }
    
    0 讨论(0)
  • 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)
    }
    

    If anyone wants some copy pasta, this is how you do it from a custom UIPresentationController

    0 讨论(0)
  • 2020-12-29 23:47

    In touch began you should write like

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-29 23:49

    You can use the below function some this check on the view name will not work but if you have more than one view the problem you will see that if you tap inside your second view the same action will perform. I would prefer to add tapGesture. This is the other solution.

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    
    }
    
    0 讨论(0)
提交回复
热议问题