Add a double click gesture recognizer to Swift
- 2020-06-12 10:46:40
- OfStack
You have completed the click recognizer, but you cannot figure out how to change the click recognizer to double-click.
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)
}
}
The solution
Finally, extensions solved the problem:
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")
}
}
conclusion
This is how to add double click recognizer to Swift. I hope this article can help you solve the problem of how to add double click recognizer to Swift.