I have already accomplished a single tap recognizer but can not figure out how to make that single tap recognizer a double tap instead. I could use some guidance.
Code:
import Foundation
import UIKit
class MainBoardController: UIViewController{
let tap = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "GotoProfile")
swipe.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipe)
tap.addTarget(self, action: "GotoCamera")
view.userInteractionEnabled = true
view.addGestureRecognizer(tap)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func GotoProfile(){
self.performSegueWithIdentifier("Profilesegue", sender: nil)
}
func GotoCamera(){
self.performSegueWithIdentifier("Camerasegue", sender: nil)
}
}
Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.
onTapGesture { print("VStack tapped") } } } In this situation SwiftUI will always give the child's gesture priority, which means when you tap the text view above you'll see “Text tapped”.
UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.
// Single Tap
let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PostDetailViewController.handleSingleTap(sender:)))
singleTap.numberOfTapsRequired = 1
self.viewTop.addGestureRecognizer(singleTap)
// Double Tap
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PostDetailViewController.handleDoubleTap))
doubleTap.numberOfTapsRequired = 2
self.viewTop.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
singleTap.delaysTouchesBegan = true
doubleTap.delaysTouchesBegan = true
func handleSingleTap(sender: AnyObject?) {
print("Single Tap!")
}
func handleDoubleTap() {
print("Double Tap!")
}
Magic in below three lines
singleTap.require(toFail: doubleTap)
singleTap.delaysTouchesBegan = true
doubleTap.delaysTouchesBegan = true
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")
}
}
Try Below Code
import UIKit
class MainBoardController: UIViewController{
let tap = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "GotoProfile")
swipe.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipe)
// DOUBLE TAP
tap.numberOfTapsRequired = 2
tap.addTarget(self, action: "GotoCamera")
view.userInteractionEnabled = true
view.addGestureRecognizer(tap)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func GotoProfile(){
self.performSegueWithIdentifier("Profilesegue", sender: nil)
}
func GotoCamera(){
self.performSegueWithIdentifier("Camerasegue", sender: nil)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With