pick up all UIGestureRecognizer events in callback? - ios

Is there a way to pickup all UIGestureRecognizer events in one method? (besides via directing all their selectors to the same method).
So for example:
// Add Gesture Recogniser (Long Press)
let longPressGR = UILongPressGestureRecognizer(target: self, action: #selector(GcMapView.longPressAction(_:)))
longPressGR.minimumPressDuration = 1
self.addGestureRecognizer(longPressGR)
// Add Gesture Recogniser (Pan)
let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GcMapView.panAction(_:)))
mapDragRecognizer.delegate = self
self.addGestureRecognizer(mapDragRecognizer)
// Add Gesture Recogniser (Pinch)
let pinchGestureRecogniser = UIPanGestureRecognizer(target: self, action: #selector(GcMapView.pinchAction(_:)))
pinchGestureRecogniser.delegate = self
self.addGestureRecognizer(pinchGestureRecogniser)
// SOME METHOD NOW TO PICKUP ALL EVENTS
func PICKUPALLEVENTS (sender:UIGestureRecognizer) {
print("(String(gestureRecognizer.dynamicType) - \(gestureRecognizer.state.hashValue) ")
}

No, I don't think there is any way to do that.

Have you tried adding a UIGestureRecognizer to your target, then checking the dynamic type of the UIGestureRecognizer in the selector called?

Related

Pan, Tap, and Long Press recognizers work, but Swipe gestures do not

I have tested the following code, and only the selectors for the pan, tap, and long press recognizers activate. I have tried adding the recognizers inside the UIView subclass and adding the recognizers as parameters. My only guess would be that the pan, tap, and long press are somehow gobbling up all of the touches, but I would expect only one of them to work in that case. Since the same procedures are followed for all recognizers, I don't really understand what the problem would be. Thanks in advance for any help.
init(level: LevelView) {
self.level = level
left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
up = UISwipeGestureRecognizer(target: self, action: #selector(swipeUp))
down = UISwipeGestureRecognizer(target: self, action: #selector(swipeDown))
tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
pan = UIPanGestureRecognizer(target: self, action: #selector(pan(_:)))
long = UILongPressGestureRecognizer(target: self, action: #selector(long(_: )))
long.minimumPressDuration = 0.3
long.allowableMovement = 30
left.direction = .left
right.direction = .right
up.direction = .up
down.direction = .down
for rec in [up, down, right, left, tap, long, pan] {
rec!.cancelsTouchesInView = false
level.addGestureRecognizer(rec!)
}
}
Update:
The Pan recognizer was immediately recognizing the touches and not passing them to the swipe recognizers. Since I wanted the swipe recognizers to take precedence, I added the condition that all of the swipe gestures fail before the pan recognizer could recognize. That worked. Thank you to Matt for the helpful tip.

Detecting Swipe Gestures on TableView

Is there a way to detect up & down swipes on TableView in a ViewController?
in ViewDidLoad();
let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeUp:"))
let downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeDown:"))
upSwipe.direction = .Up
downSwipe.direction = .Down
tableView.addGestureRecognizer(upSwipe)
tableView.addGestureRecognizer(downSwipe)
Unfortunately, this doesn't work.
func swipeDown() {
print("A")
}
nothing returned.
What is the appropriate way to detect gestures on TableView (not cell/as a whole)?
Implement UIScrollViewDelegate
Define scrollViewDidScroll(_:)
Set your tableview's scrollview's delegate to whatever object you've got the above defined in.

Add single, double and triple tap gestures to all "buttons" in custom keyboard in swift

I'm trying to create a keyboard the allows single tap, double tap, and triple tap. So I want to add a UITapGestureRecognizer() to each button in my keyboard. I know how to do this manually from the xib file (add each letter it's own gesture, which would take ages) but not quite sure how to do it in the Controller.
I wrote this for double tap in the viewDidLoad() method:
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTap.numberOfTapsRequired = 2
for button in self.view.subviews{
button.addGestureRecognizer(doubleTap)
}
and created a doubleTapCharacter() method but it's still not working. I also want to be able to send information to the doubleTapCharacter method.
Any help would be much appreciated. Also, I'm very new to swift so if the instructions are complicated, I'd highly appreciate it if you can break it down a little.
create and add the gesture recognizers:
for button in view.subviews {
// create the gesture recognizer
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTapRecognizer.numberOfTapsRequired = 2
// add gesture recognizer to button
button.addGestureRecognizer(doubleTapRecognizer)
}
then implement the target method:
func doubleTapCharacter(doubleTapRecognizer: UITapGestureRecognizer) {
let tappedButton = doubleTapRecognizer.view as! UIButton
print(tappedButton.titleForState(UIControlState.Normal))
}

Distinguish a single and double tap on UITableViewCell

I added an UITapGestureRecogniser to my tableViewCell which recognizes if the user double tapped a cell. If I double tap the cell, the function didSelectRowAtIndexPath gets automatically called. I just want that the func of the gesturerecogniser gets called if the user do a double tap on a cell and not both. Does someone have an idea to solve this?
In viewDidLoad function :
let aSelector : Selector = “start:”
let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
tapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(tapGesture)
let bSelector : Selector = “stop:”
let doubleTapGesture = UITapGestureRecognizer(target: self, action: bSelector)
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)
tapGesture.requireGestureRecognizerToFail(doubleTapGesture)
The action aSelector will be called on single tap and bSelector on double tap.

UITapGestureRecognizer with tap event

I am trying to fire event with UITapGestureRecognizer on different uiview but it is not working.
var tap = UITapGestureRecognizer(target: self, action: Selector("tappedMe"))
AUIView.addGestureRecognizer(tap)
AUIView.tag = 1
BUIView.addGestureRecognizer(tap)
BUIView.tag = 2
func tappedMe()
{
if AUIView.tag == 1
{
println("1")
}
else if BUIView.tag == 2
{
println("2")
}
}
You cannot add the same gesture recognizer to multiple views. This answer explains why.
Either declare a new gesture recognizer, or create a copy of the existing one before adding it to the other view.
BUIView.addGestureRecognizer(tap.copy())

Resources