Reputation: 1391
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)
}
}
Upvotes: 17
Views: 28225
Reputation: 784
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")
}
}
Upvotes: 14
Reputation: 3813
More Swifty way:
view?.tap(numberOfTapsRequired: 2, action: action)
Closure:
private lazy var action: Action = Action(action: {[weak self] _ in
print("Double click")
})
It's very important not to lose reference to your action (you can make it as field like in above example).
by using this UIView extension: https://gist.github.com/yoman07/7aae5abb957ff656b2f118ad1e6c2d36
Upvotes: 0
Reputation: 2725
// 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
Upvotes: 31
Reputation: 958
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)
}
}
Upvotes: 9
Reputation: 7205
Here is some example code for a single and double tap action.
Notice that the single tap action has a colon (handleSingleTap:
) but the double tap action does not (handleDoubleTap
)? This indicates that the selector takes an argument. So in the functions below, we can check the sender of the single tap which would be useful if you were assigning the tap function to many elements. If you have the colon you must have the parameters in the function.
override func viewDidLoad() {
super.viewDidLoad()
// Single Tap
let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
singleTap.numberOfTapsRequired = 1
self.view.addGestureRecognizer(singleTap)
// Double Tap
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleDoubleTap")
doubleTap.numberOfTapsRequired = 2
self.view.addGestureRecognizer(doubleTap)
}
func handleSingleTap(sender: AnyObject?) {
print("Single Tap!")
}
func handleDoubleTap() {
print("Double Tap!")
}
Note: this example will run the single tap action in a double tap.
Upvotes: 6