I am using both a tap and pan gesture in my View. The view has a UICollectionView where I am trying to call didSelectItemAthowever the method is not called.
I have tried the following, but with no luck.
override func viewDidLoad() {
panGesture.delegate = self
tapGesture.delegate = self
}
extension AddNotebookViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
Does anybody have any idea what the issue may be ?
The problem, as you've already guessed, is that the background view's gesture recognizer swallows the tap that would select the collection view cell. To solve the problem, implement this gesture recognizer delegate method in your view controller:
func gestureRecognizerShouldBegin(_ gr: UIGestureRecognizer) -> Bool {
let p = gr.location(in: self.view)
let v = self.view.hitTest(p, with: nil)
return v == gr.view
}
The result is that if the gesture is in the collection view, the background view's gesture recognizer won't begin and normal selection will be able to take place.
Related
I have an app that manipulates view with gestures. View is connected to pan, pinch and rotate gestures. If I start interacting with the view with two fingers all gestures are working simultaneously (expected behavior). But if you start with one finger pan, pinch and rotate gestures do not work. None of the methods in the delegate are called when I am trying to start pinch or rotate while panning.
shouldRecognizeSimultaneouslyWith
is always true.
Expected behavior,
one finger pan on the view
add second finger and start pinch/rotate interaction
(similar to instagram stories editor)
From a clean project I added the following:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.addGestureRecognizer({
let gesture = UIPanGestureRecognizer(target: self, action: #selector(onPan))
gesture.delegate = self
return gesture
}())
view.addGestureRecognizer({
let gesture = UIPinchGestureRecognizer(target: self, action: #selector(onPinch))
gesture.delegate = self
return gesture
}())
view.addGestureRecognizer({
let gesture = UIRotationGestureRecognizer(target: self, action: #selector(onRotate))
gesture.delegate = self
return gesture
}())
}
#objc private func onPan(_ sender: UIPanGestureRecognizer) {
print("Did pan to \(sender.translation(in: sender.view))")
}
#objc private func onPinch(_ sender: UIPinchGestureRecognizer) {
print("Did pinch to \(sender.scale)")
}
#objc private func onRotate(_ sender: UIRotationGestureRecognizer) {
print("Did rotate to: \(sender.rotation)")
}
}
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { true }
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { true }
}
All 3 methods report changes after the scenario you described:
I started with single touch drag gesture
I pressed another finger and started rotating gesture and pinching gesture
Does already this example not work for you? What is the behavior you are experiencing and what is expected behavior?
I am using LNPopupController framework for presenting View Controllers, the presented VC can be dismissed by dragging it down, however I have added a collection view in this VC and dragging finger in its area doesn't drag the VC down. I want this CollectionView to scroll horizontally, I tried subclassing UICollectionView and use the
gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
method but it doesn't make any difference.
Here is my code:
class MyCollectionView: UICollectionView, UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
in viewDidLoad()
func setupCollection() {
cellSize = CGSize(width: self.view.frame.width, height: self.view.frame.width)
collectionView.delegate = self
collectionView.dataSource = self
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
}
//collectionView.isScrollEnabled = false
collectionView.isPagingEnabled = true
collectionView.alwaysBounceVertical = false
}
when scrolling is disabled everything works perfectly, I can close the view by dragging the CollectionView's area.
First, I would implement UIGestureRecognizerDelegate in the view controller that contains the collection view:
extension MyViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
Then you also need to set this delegate to the proper gesture recognizer - in your code I do not see any such line that would assign the delegate to none of the gesture recognizers. I would assign it to the gesture recognizer that is responsible for dismissing the viewController:
self.popupContentView.popupInteractionGestureRecognizer.delegate = self
Call this line of code in viewDidLoad of the MyViewController (I use this name since you did not mention the name of your controller). Here I assume that popupContentView.popupInteractionGestureRecognizer is the pan gesture recognizer that dismisses the view controller (I checked quickly LNPopupController).
Author of LNPopupController here.
The other answer is correct. You need to implement the gesture recognizer's delegate and exclude it from interaction (or do even more complex stuff).
To do this, set the controller.popupContentView.popupInteractionGestureRecognizer.delegate property. Make sure controller is the actual controller you call presentPopupBarWithContentViewController:animated:completion: on.
I am using library call Page-Menu (https://github.com/PageMenu/PageMenu) which allow me to add child view controllers in view controller. i can scroll between this view controllers.In one of the child view controller I'm using UITableView.i am using UITableViewRowAction to delete cell but. Don't know why but table not detecting swipe gesture. if i disable Page-menu scrolling then table-view UITableViewRowAction working. i searched lots about this in google. i didn't find any solution. Thanks
Having played a bit with the library, I found the issue is to do with disabling scroll view gesture recogniser in the CAPSPageMenu view controller.
Follow these steps to enable table view editing in one of the child view controllers.
Subclass a UIScrollView like this for the PageMenu target:
class CustomScrollView : UIScrollView, UIGestureRecognizerDelegate {
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
gestureRecognizer.cancelsTouchesInView = false
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return ((self.delegate as! CAPSPageMenu).gestureDelegate?.gestureRecognizerShouldRecognizeSimultaneouslyWith(gestureRecognizer, otherGestureRecognizer: otherGestureRecognizer))!
}
}
Then in CAPSPageMenu, replace:
let controllerScrollView = UIScrollView()
with
let controllerScrollView = CustomScrollView()
Declare a protocol in the same file:
#objc public protocol CustomGestureDelegate {
func gestureRecognizerShouldRecognizeSimultaneouslyWith(_ gestureRecognizer: UIGestureRecognizer, otherGestureRecognizer: UIGestureRecognizer) -> Bool
}
And add this property to CAPSPageMenu:
open weak var gestureDelegate: CustomGestureDelegate?
Here I have made controllerArray as a property in ViewController so we can filter out the view controllers with a table view later.
var controllerArray : [UIViewController] = []
Then make ViewController conform to CustomGestureDelegate with ViewController: UIViewController, CustomGestureDelegate, and set this in viewDidLoad:
pageMenu!.gestureDelegate = self
Implement this function in ViewController (your container view controller):
func gestureRecognizerShouldRecognizeSimultaneouslyWith(_ gestureRecognizer: UIGestureRecognizer, otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if pageMenu?.currentPageIndex == 0 {
return (controllerArray[0] as! CustomGestureDelegate).gestureRecognizerShouldRecognizeSimultaneouslyWith(gestureRecognizer, otherGestureRecognizer: otherGestureRecognizer)
}
return true
}
You can add similar logic for other child view controllers with table views. Here I assumed only the initial view controller had a table view.
Make your child view controllers conform to CustomGestureDelegate by adding the following implementation:
class TestTableViewController: UITableViewController, CustomGestureDelegate {
func gestureRecognizerShouldRecognizeSimultaneouslyWith(_ gestureRecognizer: UIGestureRecognizer, otherGestureRecognizer: UIGestureRecognizer) -> Bool {
let location = gestureRecognizer.location(in: self.view)
let path = self.tableView.indexPathForRow(at: location)
if path != nil {
gestureRecognizer.isEnabled = false
}
gestureRecognizer.isEnabled = true
return true
}
}
I hope this helped you.
UPDATE:
Check out https://github.com/LemonSpike/PageMenu to see a demo of this for PageMenuDemoStoryboard target.
I have a "search" UITextField at the top of my view.
Below this, I have a UICollectionView which is populated with the search results as the user types.
When a user is typing into the UITextView, the keyboard is displayed. At first, I wanted to hide the keyboard if the user touched anywhere outside the UITextField. I accomplished this with the following:
func textFieldDidBeginEditing(textField: UITextField) {
if (textField == self.textFieldSearch) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFieldSearchDidChange:", name: UITextFieldTextDidChangeNotification, object: textField)
}
var tapGesture = UITapGestureRecognizer(target: self, action: "dismissKeyboard:")
self.view.addGestureRecognizer(tapGesture)
}
func dismissKeyboard(gesture: UIGestureRecognizer) {
self.textFieldSearch.resignFirstResponder()
self.view.removeGestureRecognizer(gesture)
}
However, if the user taps on a UICollectionViewCell, the dismissKeyboard func runs, and hides the keyboard, but the user has to tap on the cell again to run the func:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
How do I do this all in one step? So if the user touches anywhere outside the UITextField, hide the keyboard...but if the user happens to touch a UICollectionViewCell, run the didSelectItemAtIndexPath function on the first touch as well as hide the keyboard and end editing on the UITextField?
Any help is appreciated!
Thanks
Please try this one
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if touch.view == self.textFieldSearch{
return false;
}
else{
return true;
}
}
and add this line in your code
var tapGesture = UITapGestureRecognizer(target: self, action: "dismissKeyboard:")
tapGesture.delegate = self // add gesture delegate here
self.view.addGestureRecognizer(tapGesture)
You can implement the delegate method of UIGestureRecognizerDelegate
optional func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldReceiveTouch touch: UITouch) -> Bool
Above method returns NO for the views on which gesture recognizer should not be called and in your case it should be collection view else for other things return YES.
example implementation-
/**
Disallow recognition of tap gestures on the collection view.
*/
func gestureRecognizer(recognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
{
if touch.view == collectionView && recognizer == tapRecognizer {
return false
}
return true
}
Here's a solution. A little janky but it should do the trick. You might have to tinker with it a little. Also my Swift isn't top notch so excuse any syntax errors.
func dismissKeyboard(gesture: UIGestureRecognizer) {
let point : CGPoint = gesture.locationInView(self.collectionView)
let indexPath : NSIndexPath = self.collectionView.indexPathForItemAtPoint(point)
self.textFieldSearch.resignFirstResponder()
self.view.removeGestureRecognizer(gesture)
self.collectionView.selectItemAtIndexPath(indexPath, animated:YES, scrollPosition:UICollectionViewScrollPosition.Top)
}
I am providing here a Objective-C code to dismiss keyBoard on any touch performed outside. Kindly write it in Swift for yourself. The problem is you are adding A tap Gesture which is hiding the touch events of your collection cell.
So instead of tap gesture use the Swift-translated code of the following code.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
Dont use your tap gesture. And I think things should get going for you...
EDIT
Some efforts to write the above code in swift for you...
override func touchesBegan(touches: NSSet, withEvent event: UIEvent){
self.view.endEditing(true)
}
This is the code that ended up finally working, with the help of #Sanjay above.
I still wanted to hide the keyboard if the user touched on the part of the collectionView that wasn't occupied by cells. I will still have to implement code for hiding the keyboard if a user 'swipes/scrolls/drags' on the UICollectionView instead of taps.
For now:
func gestureRecognizer(recognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
for cell:AnyObject in self.collectionView.visibleCells() {
if (touch.view.isDescendantOfView(cell as UIView)) {
self.resignAllResponders()
return false
}
}
return true
}
In my ViewController I have a UIScreenEdgeGestureRecognizer for switching views but also a UITableView with a custom UITableViewCell. In this custom UITableViewCell is a UIPanGestureRecognizer for swiping the cells.
I added gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: so both of the gestures are working and gestureRecognizerShouldBegin to prevent the conflict of vertical scrolling.
The question is, how can I give the UIScreenEdgeGestureRecognizer priority? When I swipe at the edge of the screen, I want to switch the views without panning the cells. I figured I should be using the delegate methods, but so far the more I read the more confused I'm getting.
Code in custom UITableViewCell:
override func viewDidload() {
var recognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
recognizer.delegate = self
addGestureRecognizer(recognizer)
}
override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if let panGesture = gestureRecognizer as? UIPanGestureRecognizer {
let velocity = panGesture.velocityInView(superview!)
if fabs(velocity.x) > fabs(velocity.y) { return true }
return false
}
return false
}
ViewController:
override func viewDidLoad() {
let rightScreenEdgeRecognizer = EdgePanGesture(target: self, action: "changeView:")
rightScreenEdgeRecognizer.edges = .Right
view.addGestureRecognizer(rightScreenEdgeRecognizer)
}
I also tried identifying the recognizers with:
gestureRecognizer.isKindOfClass(UIScreenEdgePanGestureRecognizer)
and
gestureRecognizer as? UIScreenEdgePanGestureRecognizer
But all were failed.
Turns out I was looking at the right direction with gestureRecognizer as? UIScreenEdgePanGestureRecognizer but instead of gestureRecognizer I should have used otherGestureRecognizer.
override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if let recognizer = otherGestureRecognizer as? UIScreenEdgePanGestureRecognizer {
return true
}
return false
}