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.
Related
I'm trying to add UILongPressGesture to the cell. It is working but only when I long press and move in any direction.
It should call the selector method on long press but it is calling when I long press and started to move. I'm also handling the state of the gesture but selector is not calling until I long press and start moving.
I have also tried with adding gesture to cell's content view and its UIlable element, but no luck.
let longPressGesture: UILongPressGestureRecognizer = {
let gesture = UILongPressGestureRecognizer()
gesture.addTarget(self, action: #selector(MyViewController.handleLongPressGetureForRow(_:)))
gesture.delaysTouchesBegan = false
gesture.cancelsTouchesInView = false
gesture.numberOfTouchesRequired = 1
gesture.minimumPressDuration = 0.2
return gesture
}()
cell.addGestureRecognizer(longPressGesture)
cell.tag = indexPath.row
Cells where adding gesture
Please help me. Thanks in advance.
Instead of handleLongPressGetureForRow(_:) change to self.handleLongPressGetureForRow(v:) with #objc before func
let longPressGesture: UILongPressGestureRecognizer = {
let gesture = UILongPressGestureRecognizer()
gesture.addTarget(self, action: #selector(self.handleLongPressGetureForRow(v:)))
gesture.delaysTouchesBegan = false
gesture.cancelsTouchesInView = false
gesture.numberOfTouchesRequired = 1
gesture.minimumPressDuration = 0.2
return gesture
}()
cell.addGestureRecognizer(longPressGesture)
cell.tag = indexPath.row
return cell
}
#objc func handleLongPressGetureForRow(v: UILongPressGestureRecognizer )
{
print("saghsaghghsgfsgsaghghsaghsaghghsaghashgsasa")
}
I want to override the default behavior of double tapping the mapView.
In my swift app I have a mapView in a static cell, so in the method cellForRowAt I've decided to add a UITapGestureRecognizer. This is how I do it:
func tableView(_ myTable: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath as NSIndexPath).row == 0 {
let cell = myTable.dequeueReusableCell(withIdentifier: "cellStatic") as! MyTableDetailsCell
cell.mapView.isScrollEnabled = false //this works
//this does not:
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
cell.mapView.addGestureRecognizer(tap)
...
And then I have a simple function:
func doubleTapped() {
print("map tapped twice")
}
But when I tap twice the map - it zooms in and there's no print in the console log - so my code doesn't work. What did I miss?
You had to enforce that your own double tap gesture recognizer disables the standard double tap gesture recognizer of the mapView.
This can be done using a delegate method:
Declare your view controller as a delegate of a gesture recognizer, using UIGestureRecognizerDelegate
Define a property for your own double tap gesture recognizer:
var myDoubleTapGestureRecognizer: UITapGestureRecognizer?
Set up your double tap gesture recognizer, e.g. in viewDidLoad:
myDoubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
myDoubleTapGestureRecognizer!.numberOfTapsRequired = 2
myDoubleTapGestureRecognizer!.delegate = self
mapView.addGestureRecognizer(myDoubleTapGestureRecognizer!)
Note, that the delegate is set here.
Implement the following delegate method:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if ((gestureRecognizer == myDoubleTapGestureRecognizer) && (otherGestureRecognizer is UITapGestureRecognizer)) {
let otherTapGestureRecognizer = otherGestureRecognizer as! UITapGestureRecognizer
return otherTapGestureRecognizer.numberOfTapsRequired == 2
}
return true
}
So, when you double tap the mapView, this delegate method returns true if the other gesture recognizer is the built-in double tap recognizer of the mapView. This means that the built-in double tap recognizer can only fire if your own double tap recognizer fails to recognize a double tap, which it won’t.
I tested it: The map is no longer zoomed, and method doubleTapped is called.
Try using touchesBegan to identify the touch event, and you can call your custom handler when the event is trigerred
Add the mapview as a subview of a container view in the tableViewCell. Set constraints so that the mapview fills the entier container view. Disable the user interaction of the mapview and add the double tap gesture to the container view. This code will help.
let cell = myTable.dequeueReusableCell(withIdentifier: "cellStatic") as! MyTableDetailsCell
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
cell.mapView.isUserInteractionEnabled = false
cell.containerView.addGestureRecognizer(tap)
tap.numberOfTapsRequired = 2
Now the "doubleTapped" selector will be called when the mapview is tapped twice. All other user interactions including the rotation gesture of the mapview are disabled.
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?
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.
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))
}