I want to detect 3 actions, "Tap begin", "Long press begin", "Long press ended". I want to detect "Tap begin" regardless of detection long tap (i.e. every time when touch the screen, detect "Tap begin") and detect "Tap begin" followed by "long press begin" in case keep touching.
Below code enables to detect "Tap begin" only in case "Long tap" is not detected.
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.Long(_:)))
longPressGesture.minimumPressDuration = 3
longPressGesture.allowableMovement = 30
let shortPressGesture = UITapGestureRecognizer(target: self, action: #selector(self.Tap(_:)))
touchView.addGestureRecognizer(shortPressGesture)
touchView.addGestureRecognizer(longPressGesture)
}
#objc func Long(_ sender: UILongPressGestureRecognizer) {
if(sender.state == UIGestureRecognizer.State.began) {
print("Long tap begin")
} else if (sender.state == UIGestureRecognizer.State.ended) {
print("Long tap ended")
}
}
#objc func Tap(_ sender: UITapGestureRecognizer) {
print("Tap begin")
}
You need to conform to UIGestureRecognizerDelegate
class ViewController: UIViewController, UIGestureRecognizerDelegate
and then implement the shouldRecognizeSimultaneouslyWith function to allow your two gesture recognisers to work at the same time.
Also I think you're actually wanting to use two UILongPressGesutureRecognizers as taps are detected on touch up.
#objc func Long(_ sender: UILongPressGestureRecognizer) {
if(sender.state == UIGestureRecognizer.State.began) {
print("Long tap begin")
} else if (sender.state == UIGestureRecognizer.State.ended) {
print("Long tap ended")
}
}
#objc func Tap(_ sender: UILongPressGestureRecognizer) {
if(sender.state == UIGestureRecognizer.State.began) {
print("Tap begin")
} else if (sender.state == UIGestureRecognizer.State.ended) {
print("Tap ended")
}
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == longPressGesture && otherGestureRecognizer == shortPressGesture {
return true
}
return false
}
Finally don't forget to set the delegates of the gesture recognisers to self
tapPressGesture.delegate = self
shortPressGesture.delegate = self
Related
First of all, none of the already answered questions didn't help me
Swift: Long Press Gesture Recognizer - Detect taps and Long Press
Using tap gesture and long press at the same time in Table View
Long Press Gesture Recognizer Only Fired When Finger is Lifted
And so on
The code working almost fine except one thing: the long press gesture only called when I lift my finger up from the screen. But I need to get behaviour like in Instagram Stories (when you can switch between stories and hold your finger to pause pause some story).
My question is more about how to force UILongPressGesture to fire when user touch finger down, but not up.
Here's my code:
private func setupTapGestures() {
tapRecognizer = UITapGestureRecognizer()
tapRecognizer?.addTarget(self, action: #selector(handleTapGesture(_:)))
tapRecognizer?.delegate = self
view.addGestureRecognizer(tapRecognizer!)
longPressRecognizer = UILongPressGestureRecognizer()
longPressRecognizer?.addTarget(self, action: #selector(handleLongPressGesture(_:)))
longPressRecognizer?.minimumPressDuration = 0.1
longPressRecognizer?.delegate = self
view.addGestureRecognizer(longPressRecognizer!)
}
#objc func handleTapGesture(_ gestureRecognizer: UIGestureRecognizer) {
let width = view.frame.width
let point = gestureRecognizer.location(in: view)
viewModel?.tapAction(viewWidth: width, tapPoint: point)
Swift.print("Tap gesture")
}
#objc func handleLongPressGesture(_ gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
Swift.print("Began")
} else if gestureRecognizer.state == .ended {
Swift.print("Ended")
}
}
UIGestureRecognizerDelegate:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a long-press fails
if gestureRecognizer == tapRecognizer && otherGestureRecognizer == longPressRecognizer {
return true
}
return false
}
shouldRequireFailureOf docs
Any suggestions or ideas?
I wonder if your implementation of shouldRequireFailureOf is causing an issue?
This works just fine for me (note: I used .minimumPressDuration = 0.25 because it's a little difficult to tap in under 0.1 seconds):
class GestureTestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupTapGestures()
}
private func setupTapGestures() -> Void {
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
view.addGestureRecognizer(singleTapGesture)
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGesture(_:)))
longPressGesture.minimumPressDuration = 0.25
view.addGestureRecognizer(longPressGesture)
}
#objc func handleLongPressGesture(_ gesture: UILongPressGestureRecognizer) -> Void {
if gesture.state == .began {
print("Did Long Press (began)")
}
if gesture.state == .ended {
print("Did Long Press (ended)")
}
}
#objc func handleTapGesture(_ gesture: UITapGestureRecognizer) -> Void {
print("Did Single Tap")
}
}
When I tap, I get "Did Single Tap" in the debug console.
When I tap and hold, I quickly get "Did Long Press (began)", and on finger-lift I get "Did Long Press (ended)"
I have a UIView that I added a UILongPressGestureRecognizer to so that I can handle clicks and have that UIView work like a button.
let longPressGtr = UILongPressGestureRecognizer(target: self, action:#selector(longPressSelector))
longPressGtr.minimumPressDuration = 0.1
myView.isUserInteractionEnabled = true
myView.addGestureRecognizer(longPressGtr)
#objc func longPressSelector(_ gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
myView.backgroundColor = UIColor.gray
} else if gestureRecognizer.state == .ended {
myView.backgroundColor = UIColor.blue // my button is blue
doSomething()
}
}
func doSomething() {
print("view was pressed")
}
This works, but the one thing that doesn't is when I press and hold on my UIView but drag my finger off the view, the "button" doesn't unselect. It still fires doSomething(). A regular UIButton will deselect the button and not fire it's onClick if you are holding down on it an drag your finger off the view.
How can I implement this functionality into my UIView?
Or is there a better way to make a UIView act like a button?
You need to check whether the gesture is inside the view.
#objc func longPresserDidFire(_ presser: UILongPressGestureRecognizer) {
let gestureIsInside = myView.point(inside: presser.location(in: myView), with: nil)
switch presser.state {
case .began, .changed:
if gestureIsInside {
myView.backgroundColor = .blue
} else {
myView.backgroundColor = .gray
}
case .cancelled:
myView.backgroundColor = .gray
case .ended:
myView.backgroundColor = .gray
if gestureIsInside {
doSomething()
}
default: break
}
}
You are not adding condition of gestureRecognizer state changed that's why it is accepting the end state.
#objc func longPressSelector(_ gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
} else if gestureRecognizer.state == .changed {
}else if gestureRecognizer.state == .ended {
}
}
Add one more condition and check if it works.
When you drag your finger outside of the view, gestureRecognizer is probably translated to failed or cancelled state, so you need to add handling of this cases gestureRecognizer.state == .failed and gestureRecognizer.state == .cancelled.
#objc func longPressSelector(_ gestureRecognizer: UILongPressGestureRecognizer) {
}
Place UILongPressGestureRecognizer instead of UITapGestureRecognizer and check it
I just made a simple app to try out any type of gestures. I got to the tap gesture. So I thought, what if I made a fast tap game kind of application that counts the amount of taps the user performed. But soon enough I ran into some issues.
It did not count all the taps. If I began to tap as fast as possible, but it skipped taps.
The idea is I programmatically created a view in the superview and added a tapGestureRecognizer on the view. And simply put the “taps” into a label in the app.
It seems to fail to receive a system gesture on time.
The code:
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(sender:)));
tap.numberOfTapsRequired = 1;
animationView.isUserInteractionEnabled = true;
animationView.addGestureRecognizer(tap);
The function:
#objc func tapped (sender :UITapGestureRecognizer) {
self.counter += 1;
self.lblScore.text = String(self.counter);
}
I have an animationView that I made "tappable" and it works. Every time I tap the animationView it increments the value of 'counter' that works! but every time I get this error if I tap too fast:
<_UISystemGestureGateGestureRecognizer: 0x1c01c4b00>: Gesture: Failed to receive system gesture state notification before next touch
Create Gesture :
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
panGesture.delegate = self
Button.addGestureRecognizer(panGesture)
let longTapGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongTapGesture(_:)))
Button.addGestureRecognizer(longTapGesture)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
Button.addGestureRecognizer(tapGesture)
let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(handleRotateGesture(_:)))
rotateGesture.delegate = self
Button.addGestureRecognizer(rotateGesture)
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(handlePinchGesture(_:)))
pinchGesture.delegate = self
Button.addGestureRecognizer(pinchGesture)
Gesture click Event :
extension AddTextVC: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
#IBAction func handlePanGesture(_ recognizer: UIPanGestureRecognizer) {
}
#IBAction func handlePinchGesture(_ recognizer: UIPinchGestureRecognizer) {
}
#IBAction func handleRotateGesture(_ recognizer: UIRotationGestureRecognizer)
{
}
#IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {
}
#IBAction func handleLongTapGesture(_ recognizer: UITapGestureRecognizer) {
}
}
I'm trying to make a button for Taking pictures and recording videos. When a long press is made it will record and 1 tap will take a picture. when button is being pressed i want to transform it to create a effect. however the began and ended is not being triggered since it is not transforming?
func centerButtonPressedDown(sender: UITapGestureRecognizer) {
if !pictureTaken {
delegate?.didLongTapCameraButton()
} else {
}
}
func centerButtonClicked(sender: UITapGestureRecognizer) {
if sender.state == .began {
self.centerButton.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
} else if sender.state == .ended {
self.centerButton.transform = CGAffineTransform.identity
}
}
CenterButton
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(centerButtonClicked)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(centerButtonPressedDown))
tapGesture.numberOfTapsRequired = 1
centerButton.addGestureRecognizer(tapGesture)
centerButton.addGestureRecognizer(longGesture)
I tried your code brother first.It shows me error.Then i modified the code it works fine.
let tapGesture = UITapGestureRecognizer(target: self, action:Selector("centerButtonClicked:")) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, action:Selector("centerButtonPressedDown:"))
tapGesture.numberOfTapsRequired = 1
centerButton.addGestureRecognizer(tapGesture)
centerButton.addGestureRecognizer(longGesture)
func centerButtonPressedDown(sender: UILongPressGestureRecognizer) {
if sender.state == .Began {
print("long press began")
}
else if sender.state == .Ended {
print("long press Ended")
}
}
func centerButtonClicked(sender: UITapGestureRecognizer) {
print("tap is detected")
}
When I single tap the button the printed result is
tap is detected
When I long press the button the printed result is
long press began
long press Ended
I'm having a little trouble with the gestures.
I'm trying to use both tap and long press on the same button, so I've used
#IBAction func xxx (sender: UITapGestureRecognizer)
and
#IBAction func xxx (sender: UILongPressGestureRecognizer)
but my button seems to react to both functions when I tap. What might be wrong?
func long(longpress: UIGestureRecognizer){
if(longpress.state == UIGestureRecognizerState.Ended){
homeScoreBool = !homeScoreBool
}else if(longpress.state == UIGestureRecognizerState.Began){
print("began")
}
}
Hard to say what´s not working with your code, with the only two rows that you have provided, but I would recommend you to do it in this way instead:
Create an outlet to your button instead
#IBOutlet weak var myBtn: UIButton!
And in your viewDidLoad() add the gestures to the buttons
let tapGesture = UITapGestureRecognizer(target: self, action: "normalTap")
let longGesture = UILongPressGestureRecognizer(target: self, action: "longTap:")
tapGesture.numberOfTapsRequired = 1
myBtn.addGestureRecognizer(tapGesture)
myBtn.addGestureRecognizer(longGesture)
And then create the actions to handle the taps
func normalTap(){
print("Normal tap")
}
func longTap(sender : UIGestureRecognizer){
print("Long tap")
if sender.state == .Ended {
print("UIGestureRecognizerStateEnded")
//Do Whatever You want on End of Gesture
}
else if sender.state == .Began {
print("UIGestureRecognizerStateBegan.")
//Do Whatever You want on Began of Gesture
}
}
Swift 3.0 version:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.normalTap))
let longGesture = UILongPressGestureRecognizer(target: self, action: Selector(("longTap:")))
tapGesture.numberOfTapsRequired = 1
myBtn.addGestureRecognizer(tapGesture)
myBtn.addGestureRecognizer(longGesture)
func normalTap(){
print("Normal tap")
}
func longTap(sender : UIGestureRecognizer){
print("Long tap")
if sender.state == .ended {
print("UIGestureRecognizerStateEnded")
//Do Whatever You want on End of Gesture
}
else if sender.state == .began {
print("UIGestureRecognizerStateBegan.")
//Do Whatever You want on Began of Gesture
}
}
Updated syntax for Swift 5.x:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap))
button.addGestureRecognizer(tapGesture)
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
button.addGestureRecognizer(longGesture)
#objc func normalTap(_ sender: UIGestureRecognizer){
print("Normal tap")
}
#objc func longTap(_ sender: UIGestureRecognizer){
print("Long tap")
if sender.state == .ended {
print("UIGestureRecognizerStateEnded")
//Do Whatever You want on End of Gesture
}
else if sender.state == .began {
print("UIGestureRecognizerStateBegan.")
//Do Whatever You want on Began of Gesture
}
}