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)
And I want to remove blue UIView 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 add shouldReceiveTouch function to UIGestureRecognizerDelegate. What am I doing wrong?
Add UIGestureRecognizer to the super view :
As you said you have image view as a background.
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped(gestureRecognizer:)))
imageView.addGestureRecognizer(tapRecognizer)
tapRecognizer.delegate = self
Adding target action :
func tapped(gestureRecognizer: UITapGestureRecognizer) {
// Remove the blue view.
}
And then inside UITapGestureRecognizerDelegate :
extension ViewController : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if touch.view!.superview!.superclass! .isSubclass(of: UIButton.self) {
return false
}
return true
}
}
Hope it helps !
EDIT
Make sure that user can touch on the view by enabling : self.view.userInteractionEnabled = true
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
you can use UITapGestureRecognizer for that view
override func viewDidLoad() {
super.viewDidLoad()
// Add "tap" press gesture recognizer
let tap = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
tap.numberOfTapsRequired = 1
backgroundimage.addGestureRecognizer(tap)
}
// called by gesture recognizer
func tapHandler(gesture: UITapGestureRecognizer) {
// handle touch down and touch up events separately
if gesture.state == .began {
} else if gesture.state == .ended {
}
}
You can
subclass with that background view, and implement an inherited method -beganTouches:(there is another para but I forget what it is)
Add a UITapGestureRecognizer to that view
Add a giant button that covers the entire screen under all the buttons. Anytime the user presses on the giant button underneath the smaller buttons, do what you want it to do.
Not the most elegant but it works :P
Related
I have a problem where I have a UIScrollView and a Header (UIView) inside my main View and my Header is over my UIScrollView as such:
UIView.
|
|- UIScrollView.
|
|- Header. (UIView)
I want my header to be able to detect taps on it, but I also want my scroll view to be able to scroll when I drag over my Header which right now it is not possible because my Header is over it and is blocking the scroll.
To sum up, I want my Header to detect taps but forward scrolls to my UIScrollView.
To tackle this problem I tried multiple things, and here are some of them:
Adding a UIPanGestureRecognizer to my Header so it is able to detect dragging
Adding a UITapGestureRecognizer to my Header so it is able to detect tapping
Setting isUserInteractionEnabled = false when dragging begins so the gesture can be passed to the next UIResponder which in this case is my UIScrollView
Setting isUserInteractionEnabled = true once my dragging has finished so it can again detect tapping
This is the code snippet:
override func viewLoad() {
myScreenEdgePanGestureRecognizer = UIPanGestureRecognizer(target: self, action:#selector(handlePan))
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(handleTap(_:)))
headerView.addGestureRecognizer(myScreenEdgePanGestureRecognizer)
headerView.addGestureRecognizer(tapGestureRecognizer)
}
#objc func handlePan(_ sender: UITapGestureRecognizer){
print("dragging")
if headerView.isUserInteractionEnabled{
headerView.isUserInteractionEnabled = false
}
if sender.state == .began {
} else if sender.state == .ended {
headerView.isUserInteractionEnabled = true
}
}
#objc func handleTap(_ sender: UITapGestureRecognizer){
print("tapped")
}
At this point I see how dragging and tapping are being detected just fine, but for some reason isUserInteractionEnabled = false seems to not be changing how the view is behaving. This code is acting as isUserInteractionEnabled is always true no mater what.
Things that I have also tried besides this:
overriding the hitTest function inside UIButton
overriding touchesBegan, touchesMoved, touchesEnded methods overriding next
setting the variable to return ScrollView as the next UIResponder
setting the isExclusiveTouch method in UIButton
changing the isUserInteractionEnabled in every way possible
I was struggling with this problem too, you should try to use methods of UIGestureRecognizerDelegate which allows you to handle simultaneous gestures.
Connect your gesture recognizers delegates e.g. tapGestureRecognizer.delegate = self
Make your ViewController conform this protocol e.g.
extension YourViewController: UIGestureRecognizerDelegate {}
Implement this function:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true }
So what I have is I have a UITapGestureRecognizer for a Tap on the view (lets call that base view) and a UILongPressGestureRecognizer on that same base view. When the user taps the view action one is dispatched and if the user holds down on the view another view pops up. This behavior works nicely.
What I want to do is that the user does not have to separately tap the popup. Instead I want the user to just "pull up" on the popup and activate an action.
I tried to use a UIPanGesture on the popup but that did not work.
Edit
I dragged the long press gesture recognizer on to the view (actually a key in a custom keyboard). This is the code
#IBAction func popupPressed(_ sender: UITapGestureRecognizer) {
//Todo code for popupKey
}
#IBAction func keyPressedLong(_ sender: UILongPressGestureRecognizer) {
switch sender.state {
case .began:
popupTint = UIView(frame: self.frame)
popupTint!.backgroundColor = UIColor.black
popupTint!.alpha = 0.6
self.view.addSubview(popupTint!)
popup = UIView(frame: sender.view!.frame)
popup.frame = CGRect(x: popup.frame.origin.x, y: sender.view!.frame.origin.y - popup.frame.height, width: popup.frame.width, height: popup.frame.height)
popupKey.layer.cornerRadius = 5
popupKey.clipsToBounds = true
let popUpTap = UITapGestureRecognizer(target: self, action: #selector(self.popupPressed(tapGesture:)))
popupKey.addGestureRecognizer(popUpTap)
self.view.addSubview(popup)
default:
break
}
}
Any Ideas ?
Thanks a lot in advance.
UILongPressGestureRecognizer is continuous so you could use the location in the base view to move the popup & check if the movement threshold was met.
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.didLongPress(gesture:)))
self.baseView.addGestureRecognizer(longPress)
#objc func didLongPress(gesture: UILongPressGestureRecognizer) {
if self.popUpView.bounds.contains(gesture.location(in: self.popUpView)) {
// perform action
}
}
I hope this answers your question.
I have created a custom view that is to be used as a radio button with images and text. I need to be able to load the saved selection when the controller loads. I set my listeners this way:
for button in genderButtons {
button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(genderTapped(_:))))
}
#objc private func genderTapped(_ sender: UITapGestureRecognizer) {
for button in genderButtons {
button.select(sender.view! == button) // Toggles the button to display selected/deslected state.
...
}
}
The problem is that I can't find a way to tell the view to select. I tried making the gesture recognizer and object, but it doesn't have any methods I can use to trigger it. The 'buttons' aren't actually buttons, they're views, so I can't send an action event.
How can I select the correct button with code?
Just call genderTapped directly, handing it the gesture recognizer already attached to the desired "button".
For example, if thisGenderButton is the one you want to "tap", say:
if let tap = thisGenderButton.gestureRecognizers?[0] as? UITapGestureRecognizer {
genderTapped(tap)
}
You can add this method in your customView like this,
Class CustomView: UIView {
public func select(_ value: Bool) {
self.backgroundColor = value ? .green: .red
}
}
and then in below method you can call select for the tapped view.
#objc private func genderTapped(_ sender: UITapGestureRecognizer) {
(sender.view as? CustomView)?.select(true)
}
I have an image view on scroll view. I added more than one sub views on image view.
->UIScrollView
-->UIImageView
--->UIViews
I want to add gesture recognizer to subviews. But It doesn't work. Where is my mistake? function handleTap() not triggered.
func addSubViewOnImageView(mPoint:CGPoint, mSize: CGSize){
let rect = CGRect(origin: mPoint, size: mSize)
let sView = UIView(frame: rect)
sView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
tap.delegate = self
sView.addGestureRecognizer(tap)
imageView.addSubview(sView)
}
#objc func handleTap(_ sender: UITapGestureRecognizer) {
print("tapped any sub view")
}
This property is inherited from the UIView parent class. This class changes the default value of this property to NO.
enable the user interaction for your imageview, by default its false , for more info you get the info from apple document
imageView.isUserInteractionEnabled = true
just check
yourImageView.isUserInteractionEnabled = true
is there or not?
An Alternate if you don't want to do
yourImageView.isUserInteractionEnabled = true
Just make your UIView class a subclass of UIControl from storyboard refer from screenshot.
it will act as an UIButton no need to write the code for adding an tap gesturejust add an action and likenormally we do for UIImageView`
Good day, I have UIImageView inside UIView and I want to handle event, when user tap on UIView background, but not on UIImageView. How can I achieve it? I read about UITapGestureRecognizer, but it confuse me a little.
P.S. If u can plz answer in swift.
If you want to disable UITapGestureRecognizer on the child view of your main view then you have to enable User Interaction of your UIImageView like this
imgTest.userInteractionEnabled = true;
then you have to set the Delegate of your UITapGestureRecognizer to detect the UITouch of the view, set the delegate like this
let touch = (UITapGestureRecognizer(target: self, action: "tapDetected:"))
touch.delegate = self
self.view.addGestureRecognizer(touch)
Then you have to implement the Delegate method of UITapGestureRecognizer in which you will detect the view which is currently touched like this
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if touch.valueForKey("view")!.isKindOfClass(UIImageView) {
return false
} else {
return true
}
}
In the above code i have disable the tap gesture on UIImageView
Now, this method will work only for your self.view not for UIImageView
func tapDetected(sender: UITapGestureRecognizer) {
self.dismissViewControllerAnimated(false, completion: nil)
}
Add this line for your imageView because UIImageView default user interaction is disable. If you will enable UIImageView userinteraction then it will not work for your UIView and outside of UIImageView it will work
yourImage.userInteractionEnabled = YES;
Try this your tap clicked will work