Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

How Do I enable Tap Anywhere in the view to Dismiss ? #205

Open
WebtekDigital opened this issue Feb 15, 2021 · 4 comments
Open

How Do I enable Tap Anywhere in the view to Dismiss ? #205

WebtekDigital opened this issue Feb 15, 2021 · 4 comments

Comments

@WebtekDigital
Copy link

There should be an option to enable tap anywhere in the view to dismiss the tooltip. That's how tooltips should work, currently only tapping inside the tool tip view dismisses it, if I wanna show multiple tooltips use has to tap on each of them to dismiss which is not at all user friendly.

@Vidhanshu09
Copy link

Yeah this is a big issue in this , but i have resolved by

For iOS below 13 -

In AppDelegate -

var topWindow: CustomWindow?

In didfinishLaunching -

topWindow = CustomWindow(frame: UIScreen.main.bounds)
topWindow?.rootViewController = UIViewController()
topWindow?.windowLevel = UIWindow.Level.normal + 1
topWindow?.isHidden = false

After this create a util class -

class CustomWindow: UIWindow{
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
// Do any action you would like to perform to indicate the application is active
NotificationCenter.default.post(name: Notification.Name("eventTouch"), object: nil)
return false
}
}

Then call this observer in my class where i have to dismiss the tooltip.

NotificationCenter.default.addObserver(self, selector: #selector(self.dismisstooltip(notification:)), name: Notification.Name(rawValue: "eventTouch"), object: nil)

For iOS 13 and above

In Scene Delegate connectionoption -

let tapGesture = UITapGestureRecognizer(target: self, action: nil)
tapGesture.delegate = self
self.window?.addGestureRecognizer(tapGesture)

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
print(":hello")
NotificationCenter.default.post(name: Notification.Name("eventTouch"), object: nil)
return false
}

and Use this observer same as above.

@huy-lv
Copy link

huy-lv commented Apr 1, 2021

@Vidhanshu09 can you give an working example project

@tdopensource
Copy link

Did anyone find any working solution?
Whole component is exactly what I need, but without option to dismiss for any user action - is useless in my case

@ameedsayeh
Copy link

ameedsayeh commented Jun 17, 2021

You can subclass the Easytipview and add that functionality.

Note: make sure to call the show function you're using.

import EasyTipView

class DismissibleEasyTipView: EasyTipView {
    
    lazy var tapRecognizer: UITapGestureRecognizer = {
        
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.didTapOnScreen))
        tapRecognizer.numberOfTapsRequired = 1
        
        return tapRecognizer
    }()
    
    lazy var dismissView: UIView = {
        
        let view = UIView()
        
        view.backgroundColor = .clear
        view.frame = UIScreen.main.bounds
        
        return view
    }()
    
    func show(on view: UIView) {
        
        self.show(forView: view)
        
        guard let superView = self.superview else { return }
        
        self.addDismissView(on: superView)
    }
    
    private func addDismissView(on superView: UIView) {
        
        if self.dismissView.superview == nil {
            superView.addSubview(self.dismissView)
        }
        
        if !(self.dismissView.gestureRecognizers ?? []).contains(self.tapRecognizer) {
            self.dismissView.addGestureRecognizer(self.tapRecognizer)
        }
        
        self.tapRecognizer.isEnabled = true
    }
    
    func hide() {
        
        self.dismissView.removeFromSuperview()
        self.tapRecognizer.isEnabled = false
        self.dismiss()
    }

    @objc func didTapOnScreen() {
        self.hide()
    }
}

Use DismissableEasyTipView from now on and use show(on:) and hide()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants