I have create an date component and have problems with the GestureRecognizer.
First line is a StackView with labels. If I attach a TapGestureRecognizer it is fired on touch. The following Lines are subviews in a Stackview consisting of a label and an image view. I can add the Recognizer to the subview, to the label or the image view. It never gets fired and I made sure that userInteraction is enabled.
What could be the problem?
Here an example how I add the recognizer:
func addTap(){
dayLabel.isUserInteractionEnabled = true
imageView.isUserInteractionEnabled = true
isUserInteractionEnabled = true
tap1 = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
tap2 = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
tap3 = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
dayLabel.addGestureRecognizer(tap1!)
imageView.addGestureRecognizer(tap2!)
addGestureRecognizer(tap3!)
}
#objc func handleTap(recognizer: UITapGestureRecognizer){
logger.debug("tap")
changeSelection()
}
You can find a little demo project boiled down to the problem here: https://github.com/ogezue/datedemo
Problem I can see is your view must be cover with Image and label
and you are adding same UITapGestureRecognizer to all so that is added on last object say view (which is covered with image and label) so it may not able to get tap event
You need three different objects of UITapGestureRecognizer you can't add same tap gesture on different views
Hope it may solve your problem
For every view (Label/Imageview) you should create a tap object not the same one added to both . . .
Related
https://github.com/raylu135/Views-
I'm working on this project, I'd like to add a function that allows me to bring the tapped view to the front.
After some researches, I tried to add an UITapGestureRecognizer and view.bringSubviewToFront but it didn't work... maybe I'm not doing it the right way, how can I do?
It won't work like in the link.
1, You need to setup the tap gesture recogniser
2, You need to create an #objc function and call this on action.
//paste it into viewDidLoad.
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
tap.cancelsTouchesInView = false
//Uncomment next line, if you want more than one tap to activate gesture.
//tap.numberOfTapsRequired = 5
self.view.addGestureRecognizer(tap)
// Create a function
#objc func tapped(){
self.view.bringSubviewToFront(UIViewWhatYouWantToBringToFront)
}
if I add tapgesture on background view then I have to long press for select cell and if I set numberoftaprequired is 2 the it will be work normally but I have to give one functionality on single tap
add cancelsTouchesInView to your tap gesture
let tap = UITapGestureRecognizer(target: self, action: #selector(yourfunction))
tap.cancelsTouchesInView = false
try this
I've added a label in a view controller via the interface builder, and I've added a lot of constraints to it, but I want to replace it by a button. Can I accomplish this without losing all the constraints ? Thanks a lot in advance
I'm not sure if you can save your constraints inside interface builder. However, you can add a tap gesture recognizer to make the label perform an action when it is tapped (act like a button).
This code can help you get started:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_ :)))
myLabel.isUserInteractionEnabled = true
myLabel.addGestureRecognizer(tapGestureRecognizer)
func handleTap(_ sender: UITapGestureRecognizer) {
// perform some action when the label is tapped
}
You can look at this question for more information.
Don’t replace by button
Add tap gesture for click action
you don't need to remove that label. Just addGestureRecognizer a tap gesture on label.
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_ :)))
myLabel.isUserInteractionEnabled = true
myLabel.addGestureRecognizer(tapGestureRecognizer)
func handleTap(_ sender: UITapGestureRecognizer) {
// perform some action when the label is tapped
}
This question already has answers here:
Can you attach a UIGestureRecognizer to multiple views?
(12 answers)
Closed 5 years ago.
I have 2 UIImageView and a single tapGestureRecognizer.
override func viewDidLoad() {
// Do any additional setup after loading the view.
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
cameraUIImageView.isUserInteractionEnabled = true
cameraUIImageView.addGestureRecognizer(tapGestureRecognizer)
plus1UIImageView.isUserInteractionEnabled = true
plus1UIImageView.addGestureRecognizer(tapGestureRecognizer)
//
}
I can only tap on the second UIImageView, which is plus1UIImageView.
Why?
A UIGestureRecognizer must be used with a single view only. You are using same object for both views. Try this.
override func viewDidLoad() {
// Do any additional setup after loading the view.
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
cameraUIImageView.isUserInteractionEnabled = true
cameraUIImageView.addGestureRecognizer(tapGestureRecognizer)
let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
plus1UIImageView.isUserInteractionEnabled = true
plus1UIImageView.addGestureRecognizer(tapGestureRecognizer2)
}
You can only add a gesture recognizer to one view, so when you add it to the second it's getting removed from the first. More in depth answer here:
Can you attach a UIGestureRecognizer to multiple views?
As all ans stated you can only add a gesture recognizer to one view. Although if both of your imageviews in same super view you can add tap gesture to their superview and access its subviews. You can check for tapped subview and take reference of tapped imageview using hitTest: method. Check my previous ans here. Do let me know if you have any queries in comment.
I'm trying to create a keyboard the allows single tap, double tap, and triple tap. So I want to add a UITapGestureRecognizer() to each button in my keyboard. I know how to do this manually from the xib file (add each letter it's own gesture, which would take ages) but not quite sure how to do it in the Controller.
I wrote this for double tap in the viewDidLoad() method:
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTap.numberOfTapsRequired = 2
for button in self.view.subviews{
button.addGestureRecognizer(doubleTap)
}
and created a doubleTapCharacter() method but it's still not working. I also want to be able to send information to the doubleTapCharacter method.
Any help would be much appreciated. Also, I'm very new to swift so if the instructions are complicated, I'd highly appreciate it if you can break it down a little.
create and add the gesture recognizers:
for button in view.subviews {
// create the gesture recognizer
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTapRecognizer.numberOfTapsRequired = 2
// add gesture recognizer to button
button.addGestureRecognizer(doubleTapRecognizer)
}
then implement the target method:
func doubleTapCharacter(doubleTapRecognizer: UITapGestureRecognizer) {
let tappedButton = doubleTapRecognizer.view as! UIButton
print(tappedButton.titleForState(UIControlState.Normal))
}