i want to remove UIVIew
from screen after user tap something except that view. (to visualize it for you I will upload sketch of my view)[
]1
And I want to remove blueUIview
after user tap on something except buttons in this view. What should I use?
EDIT:
In blue UIVIew
are two buttons and I want to remove that view when user tap on background image
I did what "yerpy" told me to do but it isn't working.
func test(gestureRecognizer: UITapGestureRecognizer) {
print("test")
}
func setUpBackgroundImageView() {
self.view.addSubview(backgroundImageView)
backgroundImageView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
backgroundImageView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
backgroundImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
backgroundImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
let tap = UITapGestureRecognizer(target: self, action: #selector(test(gestureRecognizer:)))
backgroundImageView.addGestureRecognizer(tap)
tap.delegate = self
}
And I also addshouldReceiveTouch
function to UIGestureRecognizerDelegate
. What am I doing wrong?
Add
UIGestureRecognizer
to thesuper view
:As you said you have image view as a background.
Adding target action :
And then inside
UITapGestureRecognizerDelegate
:Hope it helps !
EDIT
Make sure that user can touch on the
view
by enabling :self.view.userInteractionEnabled = true
You can
-beganTouches:(there is another para but I forget what it is)
you can use UITapGestureRecognizer for that view
1- Add a view below your view, let call it overlay (gray one)
2- Add your container view with all your buttons inside (green one)
3- Add a tap gesture to the overlay (drag tap to overlay view)
4- Create a @IBAction of the tap to the viewcontroller
5- Write code to hide your green view inside the @IBAction
Image
Image