Tap Gesture isn't working. User interaction enabled. - swift

Have following view structure:
Programmatically adding tap gesture rec to Temp lbl:
let tempLblTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainFeedVC.convertDegrees))
tempLblTap.delegate = self
tempLblTap.numberOfTapsRequired = 1
tempLblTap.numberOfTouchesRequired = 1
tempLblTap.cancelsTouchesInView = false
self.tempLbl.isUserInteractionEnabled = true
self.tempLbl.addGestureRecognizer(tempLblTap)
but the method convertDegrees isn't triggered.
There are also 2 swipe gesture recognizers that are added to the same view:
let leftSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showPostPicVC))
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(leftSwipeGestureRecognizer)
let rightSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showUserVC))
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(rightSwipeGestureRecognizer)
Maybe they are the reason?

Found solution:
View that contained my lbl had its userInteractionEnabled unchecked in storyboards.
So, when adding gesture recognizers to labels, images or simply adding buttons, always check that all parent views has its userInteractionEnabled to true.

Related

How to if double tap else single tap UITapGestureRecognizer

I am trying to recognise if users tap a single tap once or double-tap
if (gestureRecognizer.numberOfTouchesRequired == 2 ){
print("Double")
}else{print("single")
}
Thank you for you advice
you will need to use two gesture recognizers for this . One to capture the single tap gestures and another for double taps .
Sample code :
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didPressPartButton))
singleTapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)

UISwipeGesture not registering first swipe

Here's my code in viewDidLoad:
//add a swipe gesture
let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(rotateDial(_:)))
downSwipe.direction = .down
self.view.addGestureRecognizer(downSwipe)
The first time I swipe down it doesn't recognize the gesture. I read in another SO that the swipe gesture is registering itself on the first swipe. How do I fix that?

Display menu list on Double Tap gesture - Swift3

I'm new to IOS and I'm trying to display list of buttons on double tap of UIView. I have the below code to capture the double tap gesture:
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.minimumPressDuration = 0.0
menuTap.numberOfTapsRequired = 2
menuTap.delaysTouchesBegan = true
self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
}
I need to design a panel to show a list of buttons such as start, stop and pause. Can anybody guide me on how to design a panel on the tapped position?
You need to use UITapGestureRecognizer for that not the UILongPressGestureRecognizer. Set UITapGestureRecognizer with numberOfTapsRequired to 2.
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.numberOfTapsRequired = 2
//No need to set isUserInteractionEnabled to true because by default it is true for `UIView`
//self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
Add action method of tapGesture like this.
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
let point = recognizer.location(in: self.view)
//Get your view from nib
let view = CustomView()
//set its origin to this point
view.frame.origin = point
//add your view in self.view
self.view.addSubview(view)
}

About UITapGestrueRecognizer in Swift

I have made a view subclass of UIView.Then, I added UITapGestureRecognizer to it but when I tap on the view, it don't respond to it.
Code:
let tapGesture = UITapGestureRecognizer(target: self, action: "singleTapHandler:")
self.maskView!.addGestureRecognizer(tapGesture)
selector:
func singleTapHandler(recognizer: UITapGestureRecognizer) {
print("xxx")
dismiss()
}
Ensure that you have enabled user interactivity:
self.maskView!.userInteractionEnabled = true
Make sure the maskView on top of the view, and the frame not equals to CGRectZero
Check if you added UIGestureRecognizerDelegate to your view, and use that tapGesture.delegate = self
Your tapGesture should be like let tapGesture = UITapGestureRecognizer(target: self, action: "singleTapHandler"), there is no need of :
Then your function can
func singleTapHandler(){
//action
}
Lastly, make sure you tap on the view you added the tap gesture.

How do I implement a UIGestureRecognizer in my app in Swift?

Lets say I have an image of a tree and when I tap on the screen it changes to an image of a car how would I do this? This is the code I have so far but its not working. (I have three images named "gp1", "gp2", and "gp3" and all of them have different number of taps to change the image.)
I'm in Spritekit and using Swift.
class GameScene: SKScene, SKPhysicsContactDelegate {
//Create Touch
createTouch()
}
override func didMoveToView(view: SKView) {
func createTouch() {
gestureRecognizer.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
gestureRecognizer == UITapGestureRecognizer(target: self, action: "gp2")
gestureRecognizer2 == UITapGestureRecognizer(target: self, action: "gp3")
gestureRecognizer3 == UITapGestureRecognizer(target: self, action: "gp1")
}
}
As Mundi said, for each UIImageView, make sure userInteractionEnabled = true. Otherwise, users won't be able to tap the image. Then, create the gestureRecognizer. Set the action to the method that will handle the gesture. Next, add the gestureRecognizer to the UIImageView. Finally, write the method to handle the gesture.
#IBOutlet var gp1: UIImageView!
#IBOutlet var gp2: UIImageView!
#IBOutlet var gp3: UIImageView!
func createTouch() {
// maker sure userInteractionEnabled is true
gp1.userInteractionEnabled = true;
gp2.userInteractionEnabled = true;
gp3.userInteractionEnabled = true;
// create the gesture recognizers
let gestureRecognizer1 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer2 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer3 = UITapGestureRecognizer(target: self, action: "tapGesture:")
// set the number of taps
gestureRecognizer1.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
// add the gesture recognizer to the UIImageViews
gp1.addGestureRecognizer(gestureRecognizer1)
gp2.addGestureRecognizer(gestureRecognizer2)
gp3.addGestureRecognizer(gestureRecognizer3)
}
Since these are all tap gestures, I've decided to use one method and a switch statement. You could, however, create different methods for each gesture if you wanted to.
func tapGesture(gesture: UITapGestureRecognizer) {
switch gesture.numberOfTapsRequired {
case 1:
gp1.image = UIImage(named: "car")
case 2:
gp2.image = UIImage(named: "car")
case 3:
gp3.image = UIImage(named: "car")
default:
break
}
}
You need to add the recognizer to the view and implement the action handlers gp2 etc.
For UIImageView you should also make sure userInteractionEnabled is set to true.