How to if double tap else single tap UITapGestureRecognizer - swift

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)

Related

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?

Tap Gesture isn't working. User interaction enabled.

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.

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 can I find button tag after implementing gesture recogniser in swift?

I want an action to be done once when someone swipes within a button.
My current code is as follows:
let recogniser = UISwipeGestureRecognizer(target: self, action: "didTapButton2:")
recogniser.direction = .Up
button.addGestureRecognizer(recogniser)
func didTapButton2(sender: UIGestureRecognizer!) {
//Here I want to be able to recognise which button this was sent from (they are all tagged)
let button = sender. as UIButton //Gives an error
I need to use gesture recognisers as opposed to UIControlEvents as I need the event to only fire once. Using this makes the event fire loads of times - just need it to go once:
button.addTarget(self, action: "didTapButton2:", forControlEvents: .TouchDragInside)
Does anyone have a solution? Thanks
A UIGestureRecognizer has a property called view which is the view that it is attached to. View is declared as an optional, so you need to unwrap it:
if let button = sender.view as? UIButton {
// use button
if button.tag == 10 {
// handle button tagged with 10
}
}