How to add a double tap Gesture Recognizer in Swift
I solved this with an extension:
override func viewDidLoad() {
super.viewDidLoad()
let tapGR = UITapGestureRecognizer(target: self, action: #selector(PostlistViewController.handleTap(_:)))
tapGR.delegate = self
tapGR.numberOfTapsRequired = 2
view.addGestureRecognizer(tapGR)
}
extension MainBoardController: UIGestureRecognizerDelegate {
func handleTap(_ gesture: UITapGestureRecognizer){
print("doubletapped")
}
}
Swift Double Tap Recognizer Responding To A Single Tap Recognizer
This may be because of some mismanagement the tap gesture is not able to add for that recognizer. You may not need to send the gesture to the selector function.
Change this
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelClicked(gesture:)))
To
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelClicked))
in addDoubleTabGestureRecognizerToWordLabel()
and remove the receiving parameter in labelClicked()
.
Hope this helps!
Adding double tap gesture recognizer to UIImageView in an UITableViewCell Swift 4+
You may need
userImage.translatesAutoresizingMaskIntoConstraints = false
as you create constraints programmatically
lazy var userImage: UIImageView = {
let newView = UIIMageView()
userImage.translatesAutoresizingMaskIntoConstraints = false
newView.layer.cornerRadius = 24
newView.layer.masksToBounds = true
newView.image = UIImage(named: "samplePic")
newView.contentMode = .scaleAspectFill
newView.isUserInteractionEnabled = true
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(myFunc))
doubleTap.numberOfTouchesRequired = 1
doubleTap.numberOfTapsRequired = 2
newView.addGestureRecognizer(doubleTap)
return newView
}()
also make it a lazy var not a computed property for being 1 instance every access , add the imageView to
self.contentView.addSubView(userImage)
and set the constraints with it
Is there a way to have a two tap gesture recognizers with different number of taps
you need to make sure that double tap gesture is failed in order to initialize tapToWatch
try this
tapToWatch.require(toFail: doubleLike)
Add single, double and triple tap gestures to all buttons in custom keyboard in swift
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))
}
Is there any property need to set-up to allow the double-tap (Swift)
Try this:
let tapRec = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(_:)))
tapRec.delegate = self
tapRec.numberOfTapsRequired = 2
view.userInteractionEnabled = true
view.addGestureRecognizer(tapRec)
And then your function:
extension YourViewController: UIGestureRecognizerDelegate {
func handleDoubleTap(_ gesture: UITapGestureRecognizer){
print("doubletapped")
}
}
Hope this helps!
Allow both single tap gesture recognizer and double tap in UIScrollView
try this code,
Objective - C
UITapGestureRecognizer *singletap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(singleTap:)];
singletap.numberOfTapsRequired = 1;
singletap.numberOfTouchesRequired = 1;
[scrollView addGestureRecognizer:singletap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired =2 ;
doubleTap.numberOfTouchesRequired = 1;
[scrollView addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
Swift 3.0
var singletap = UITapGestureRecognizer(target: self, action: #selector(self.singleTap))
singletap.numberOfTapsRequired = 1
singletap.numberOfTouchesRequired = 1
scrollView.addGestureRecognizer(singletap)
var doubleTap = UITapGestureRecognizer(target: self, action: #selector(self.doubleTap))
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
scrollView.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
Double tap in UIGestureRecognizer
To make a single and double tap gesture work together, the system, upon seeing a single, needs to know if it's really a single or the first half of a double. It must either have knowledge of the future (which would be a very valuable feature) or it must wait and see what comes next. To tell it to wait and see...
singleTap.requireGestureRecognizerToFail(doubleTap)
UITapGestureRecognizer - single tap and double tap
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
Note: If you are using numberOfTouchesRequired
it has to be .numberOfTouchesRequired = 1;
For Swift
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)
singleTapGesture.require(toFail: doubleTapGesture)
How to accelerate the identification of a single tap over a double tap?
I found the answer on this link
The swift version:
class UIShortTapGestureRecognizer: UITapGestureRecognizer {
let tapMaxDelay: Double = 0.3
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
super.touchesBegan(touches, withEvent: event)
delay(tapMaxDelay) {
// Enough time has passed and the gesture was not recognized -> It has failed.
if self.state != UIGestureRecognizerState.Ended {
self.state = UIGestureRecognizerState.Failed
}
}
}
}
With delay(delay: Double, closure:()->())
:
class func delay(delay:Double, closure:()->()) {
dispatch_after(dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}
Related Topics
Image Upload in Vapor 3 Using Postgresql
How to Get Unmanaged Object from Realm Query in Swift
How to Make Nsattributedstring Codable Compliant
Difference Between Scenedelegate and Appdelegate
Why Does an @Objc Enum Have a Different Description Than a Pure Swift Enum
In Swift, What Does This Specific Syntax Mean
Can't Use Concrete Subclass to Implement a Property in Protocol in Swift
Get Url to a Local File with Spm (Swift Package Manager)
How to Represent Magnitude for Mass in Swift
Swiftui Onhover Doesn't Register Mouse Leaving the Element If Mouse Moves Too Fast
Swift Package Manager - Type 'Bundle' Has No Member "Module" Error
Testing an Executable with Swift
Detecting If Wifi or Bluetooth Is Turned on or Off by the User
Typecasting or Initialization, Which Is Better in Swift