Gesture Recogniser for all events - ios

I have this piece of code
let window = UIApplication.sharedApplication().keyWindow
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
window?.addGestureRecognizer(tap)
It only recognizes Tap (as its tap gesture recognizer), however I want some recognizer to work for all, scroll, pinch, etc.
Also at later point of time I am removing this recognizer.
I read touches began, but how to use it on UIApplication.sharedApplication().keyWindow?
And how to remove that touches began later?

If you want to monitor all touch event then you can subclass UITapGestureRecognizer, this link should help: Monitoring all iOS touches

Related

Recognize both UILongPressGestureRecognizer and UIPanGestureRecognizer on same UIButton

I want to make a UIButton that when you long press it, it will start recording video and if you pan your finger vertically up (while still long pressing), the video will zoom in.
To my button I added a UILongPressGestureRecognizer and a UIPanGestureRecognizer that does just that. Individually, they work. However, they do not work together.
How can I make my button record when long pressing but also allow me to pan my finger and have that recognized as well? This is how I added my recognizers:
let long = UILongPressGestureRecognizer(target: self, action: #selector(record(gesture:)))
button.addGestureRecognizer(long)
let pan = UIPanGestureRecognizer(target: self, action: #selector(zoom(pan:)))
button.addGestureRecognizer(pan)
You need to confirm the delegate of those two gestures.
for ex:
let long = UILongPressGestureRecognizer(target: self, action: #selector(record(gesture:)))
long.delegate = self
button.addGestureRecognizer(long)
let pan = UIPanGestureRecognizer(target: self, action: #selector(zoom(pan:)))
pan.delegate = self
button.addGestureRecognizer(pan)
and there is a delegate method to recognize multiple gestures simultaneously.
gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
define that in your class and return true.
you will get what you want.
I know this wasn't exactly what the question was asking but you can actually bypass having to use gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) and use UILongPressGestureRecognizer as a UIPanGestureRecognizer using the UIGestureRecognizer.State changes. Thats what i've done in the past, cleans things up and makes more logical sense than having two gesture recognizers

How do I make a function that executes when a tap gesture is lifted in Swift 3.0

Is there any way that i can setup a tapGestureRecognizer in an iOS app, that either sends a signal both when an object is tapped, and when the tap is released, or setup up two tapGestureRecognizer's, one that handles the tap, and one that handles the release?
My tapGestureRecognizer is initialized like this:
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(TapInToSubView))
tapRecognizer.numberOfTapsRequired = 1
sender.addGestureRecognizer(tapRecognizer)
Hope someone out there can help!
You need to set up a UILongPressGestureRecognizer . Set the minimumPressDuration and You can then handle the gesture state methods :
(sender.state == UIGestureRecognizerStateEnded)
(sender.state == UIGestureRecognizerStateBegan
etc. and fire your actions accordingly.
Long-press gestures are continuous. The gesture begins (began) when
the number of allowable fingers (numberOfTouchesRequired) have been
pressed for the specified period (minimumPressDuration) and the
touches do not move beyond the allowable range of movement
(allowableMovement). The gesture recognizer transitions to the Change
state whenever a finger moves, and it ends (ended) when any of the
fingers are lifted.

GestureRecognizer doesn't work on TableView

I'm trying to find a way to hide the keyboard as soon as the user taps elsewhere on the screen after filtering the content of my tableview.
But somehow when I add an UITapGestureRecognizer my TableView freezes and it's impossible to go further into the application.
Here's what I do :
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(tap)
func dismissKeyboard() {
view.endEditing(true)
}
I've tried this on a Blank ViewController with a TextField and it works. What am I missing? Is there a specific way to add GestureRecognizer to table views? Because as soon I add the Gesture it breaks everything. I also tried using resignFirstResponder with the same results.
Any advice? Thanks!
Try setting the cancelsTouchesInView variable to false. This defaults to true, by setting it to false you allow the touches to be responded to by your gesture and then pass through your gesture back down to the view so the tableView can respond as well.
tap.cancelsTouchesInView = false
Documentation on Apple
Discussion Excerpt from Docs:
When this property is true (the default) and the receiver recognizes its gesture, the touches of that gesture that are pending are not delivered to the view and previously delivered touches are cancelled through a touchesCancelled:withEvent: message sent to the view. If a gesture recognizer doesn’t recognize its gesture or if the value of this property is false, the view receives all touches in the multi-touch sequence.

Long Press Gesture Recognizer Only Fired When Finger is Lifted

I'm having an interesting problem with a long press gesture recognizer. I placed one of these on a UITableView, and it only works when I lift my finger after the long press. So basically, I would place my finger on a cell, and then when I lift my finger, it triggers the long press. I figured this out by putting printns when the long press began and ended and both fire after I lift my finger. I think the tableViews default panGestureRecognizer might be interfering with the longPressGestureRecognizer. Here is my code in viewDidLoad:
var longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
longPress.minimumPressDuration = 0.06
longPress.delegate = self
self.tableView.addGestureRecognizer(longPress)
longPress.requireGestureRecognizerToFail(self.tableView.panGestureRecognizer)
Touching down in the cell will not cause the table view's panGestureRecognizer to fail, so delete the requireGestureRecognizerToFail method, and you should then get to the .Began state while your finger is still down.

iOS Combining longPress and swipe gesture

I add a swipeUp gesture to the whole view.
I add a longPressGestureRecognizer to the whole view, set its minimunPressDuration equals 0.001f so that it can both detect press down action and touches move action, then call the requireGestureToFail function:
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressed:)];
longPressGestureRecognizer.minimumPressDuration = 0.001f;
[longPressGestureRecognizer requireGestureRecognizerToFail:swipeGestureRecognizer];
The problem is:
When user press and hold (don't move) a button, the longPress gesture's state remains UIGestureStatePossible because the swipeUp gesture doesn't fail, So that it won't react to user touch.
If I don't call requireGestureRecognizerToFail all the gesture including swipeUp gesture will be recognised as longPress gesture.
Implmenting shouldRecognizeSimultaneouslyWithGestureRecognizer: is not what I expect.
What I want is when press and hold(don't move) a button, it triggers longPress, then if user swipe up it triggers swipeUp gesture, if user drags but the touch pattern doesn't fit swipeUp it still triggers longPress.
How can I implement this?
I think it would be easier to implement your own UIGestureRecognizer or UIView subclass with multiple gestures. Check this out:
UITapGestureRecognizer - make it work on touch down, not touch up?
https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html#//apple_ref/doc/uid/TP40009541-CH2-SW2

Resources