UIPanGestureRecognizer on UITableViewCell overrides UITableView's scroll view gesture recognizer - ios

I've subclassed UITableViewCell and in that class I apply a Pan gesture recogniser:
UIPanGestureRecognizer *panning = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePanning:)];
panning.minimumNumberOfTouches = 1;
panning.maximumNumberOfTouches = 1;
[self.contentView addGestureRecognizer:panning];
[panning release];
I then implement the delegate protocol which is supposed to allow simultaneous gestures in the table's view:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Then I place a log inside the handlePanning method just to see when it's detected:
- (void)handlePanning:(UIPanGestureRecognizer *)sender {
NSLog(#"PAN");
}
My problem is that I'm not able to vertically scroll through the list of cells in the tableview and that handlePanning is called no matter which direction I pan.
What I want is for handlePanning to only be called when there is only horizontal panning and not vertical. Would appreciate some guidance.

Have you tried setting pannings delegate property?
panning.delegate = /* class name with the delegate method in it */;
You'll also need to conform that class to UIGestureRecognizerDelegate.

Subclass the panning gesture recognizer and make it recognize only horizontal panning. There is a great WWDC 2010 video on the issue of custom gesture recognizers available. Actually there are two on that subject, check them out at https://developer.apple.com/videos/archive/:
Simplifying Touch Event Handling with Gesture Recognizers
Advanced Gesture Recognition

Add the gesture recogniser On tableview. From that, you can get the cell object. From there you can handle the cell Functionality. For each gesture, there will be a begin, changed, end state. So, store the begin position.
CGPoint beginLocation = [gesture locationInView:tblView]; // touch begin state.
CGPoint endLocation = [gesture locationInView:tblView]; // touch end state.
Using this point, you can get the IndexPath
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:beginPoint];
From this indexpath, you can access the cell.
UITableViewCell *cell = [tableview cellForRowAtIndexPath : indexPath];
Using this Cell object, you can handle it.

Have you tried setting the bounces property to NO?

Related

didSelectItemAtIndexPath of UICollectionView not getting called when its in uiscrollview

I have taken the UIScrollView inside that
i have taken one UIView with fixed
position and just below it taken UICollectionView which is horizontal scrolling,
then i have again UIView and then again i have taken
UICollectionView with fixed cell 1.
So, on select of item of both collection view's didSelectItemAtIndexPath method not getting called.I have found some solution here but not found exact one.
By using above solution, i am facing problem is on tap anywhere(ex. image gallary) tap of UIScrollview call the tap method but every time didSelectItemAtIndexPath called wheather i click on collection view or not default zero indexpath is called.
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gestureAction:)];
[recognizer setNumberOfTapsRequired:1];
self.scrollViu.userInteractionEnabled = YES;
[self.scrollViu addGestureRecognizer:recognizer];
-(void)gestureAction:(UITapGestureRecognizer *) sender
{
CGPoint touchLocation = [sender locationOfTouch:0 inView:self.YourCollectionViewName];
NSIndexPath *indexPath = [self.YourCollectionViewName indexPathForRowAtPoint:touchLocation];
NSLog(#"%d", indexPath.item);
}

UILabel with Gesture Recognizer inside UITableViewCell blocks didSelectRowAtIndexPath

I use some UILabels with a UITapGestureRecognizer inside a UITableViewCell. The GestureRecognizer works well. But when I tap on the label, I want that the didSelectRowAtIndexPath: should execute too. Or even just the indexPathForSelectedRow() method should give me the selected row.
Setting cancelsTouchesInView = false did not work!
Is this possible? Right now the indexPathForSelectedRow() method returns nil.
Thanks
Why are you using UITapGestureRecognizer? If you want to use that, try to set the tag of label as label.tag=indexpath.row. So you might get the value you are looking at. Regarding my own opinion, I'd remove the uitapgesturerecognizer and directly use didselectrowatindexpath method..
EDIT 2:
Try using this solution..it might help you..
-(void)handleTap:(UITapGestureRecognizer *)sender
{
CGPoint location = [sender locationInView:self.view];
if (CGRectContainsPoint([self.view convertRect:self.yourTableView.frame fromView:self.tableView.superview], location))
{
CGPoint locationInTableview = [self.yourTableView convertPoint:location fromView:self.view];
NSIndexPath *indexPath = [self.yourTableView indexPathForRowAtPoint:locationInTableview];
if (indexPath)
[self tableView:self.yourTableView didSelectRowAtIndexPath:indexPath];
return;
}
}

How can a child ignore a UIGesture in certain circumstances and let the superview handle it?

I'm implementing something similar to iOS's memory manager. It is a UICollectionView with UICollectionViewCell children. If you swipe horizontally on a cell, the parent pans left. However, if you swipe vertically, the cells move with your finger.
In my UICollectionViewCell subclass, I have:
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(didPan:)];
pan.maximumNumberOfTouches = 1;
pan.minimumNumberOfTouches = 1;
[self addGestureRecognizer:pan];
}
return self;
}
Unfortunately, now all the child cells will be handling the pan gestures. The parent never gets to handle them.
- (void)didPan:(UIPanGestureRecognizer *)gesture
{
CGPoint velocity = [gesture velocityInView:self.superview];
if (ABS(velocity.y) > ABS(velocity.x)) {
// Move the cell with the finger
} else {
// Let the parent UICollectionView pan horizontally
}
}
I already know how to move the cell with the finger, but I don't know how to do the other case: making the child cell ignore the pan gesture and letting its parent handle it.
You need to have your cell's pan gesture recognizer recognize simultaneously with your collection view's pan gesture recognizer.
There are a few ways to do this, but one way to do it is by making your cell the delegate of your pan gesture recognizer and implementing this method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
// Either just return YES to allow your cell's gesture recognizer
// to work simultaneously with all other recognizers:
return YES;
// Or you can decide whether your cell's pan gesture recognizer should
// recognize simultaneously with otherGestureRecognizer. For example,
// you could get a reference to your collection view's panGestureRecognizer
// and only return YES if otherGestureRecognizer is equal to that recognizer:
return otherGestureRecognizer == <your collection view's gesture recognizer>;
}

My gesture recognizer is attached to the wrong view

I have a UICollectionView that has elements that can be dragged and dropped around the screen. I use a UILongPressGestureRecognizer to handle the dragging. I attach this recognizer to the collection view cells in my collectionView:cellForItemAtIndexPath: method. However, the recognizer's view property occasionally returns a UIView instead of a UICollectionViewCell. I require some of the methods/properties that are only on UICollectionViewCell and my app crashes when a UIView is returned instead.
Why would the recognizer that is attached to a cell return a plain UIView?
Attaching the recognizer
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
EXSupplyCollectionViewCell *cell = (EXSupplyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
longPressRecognizer.delegate = self;
[cell addGestureRecognizer:longPressRecognizer];
return cell;
}
Handling the gesture
I use a method with a switch statement to dispatch the different states of the long press.
- (void)longGestureAction:(UILongPressGestureRecognizer *)gesture {
UICollectionViewCell *cell = (UICollectionViewCell *)[gesture view];
switch ([gesture state]) {
case UIGestureRecognizerStateBegan:
[self longGestureActionBeganOn:cell withGesture:gesture];
break;
//snip
default:
break;
}
}
When longGestureActionBeganOn:withGesture is called if cell is actually a UICollectionViewCell the rest of the gesture executes perfectly. If it isn't then it breaks when it attempts to determine the index path for what should be a cell.
First occurrence of break
- (void)longGestureActionBeganOn:(UICollectionViewCell *)cell withGesture:(UILongPressGestureRecognizer *)gesture
{
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell]; // unrecognized selector is sent to the cell here if it is a UIView
[self.collectionView setScrollEnabled:NO];
if (indexPath != nil) {
// snip
}
}
I also use other properties specific to UICollectionViewCell for other states of the gesture. Is there some way to guarantee that the recognizer will always give me back the view that I assigned it to?
Views like UICollectionView and UITableView will reuse their cells. If you blindly add a gestureRecognizer in collectionView:cellForItemAtIndexPath: you will add a new one each time the cell is reloaded. If you scroll around a bit you will end up with dozens of gestureRecognizers on each cell.
In theory this should not cause any problems besides that the action of the gestureRecognizer is called multiple times. But Apple uses heavy performance optimization on cell reuse, so it might be possible that something messes up something.
The preferred way to solve the problem is to add the gestureRecognizer to the collectionView instead.
Another way would be to check if there is already a gestureRecognizer on the cell and only add a new one if there is none. Or you use the solution you found and remove the gestureRecognizer in prepareForReuse of the cell.
When you use the latter methods you should check that you remove (or test for) the right one. You don't want to remove gestureRecognizers the system added for you. (I'm not sure if iOS currently uses this, but to make your app proof for the future you might want to stick to this best practice.)
I had a similar problem related to Long-Touch.
What I ended up doing is override the UICollectionViewCell.PrepareForReuse and cancel the UIGestureRecognizers attached to my view. So everytime my cell got recycled a long press event would be canceled.
See this answer

How to add tap gesture to UICollectionViewCell subview returned from dequeueReusableCellWithReuseIdentifier

What's the best method for efficiently adding a tap gesture to a subview of a UICollectionViewCell returned from dequeueReusableCellWithReuseIdentifier that already has a bunch of default gesture recognizers attached to it (such as a UIScrollView). Do I need to check and see if my one custom gesture is already attached (scrollView.gestureRecognizers) and if not then add it? I need my app's scrolling to be as smooth as possible so performance of the check and efficient reuse of already created resources is key. This code all takes place inside cellForItemAtIndexPath. Thanks.
I figured out a way to do it that requires only a single, shared, tap gesture recognizer object and moves the setup code from cellForItemAtIndexPath (which gets called very frequently as a user scrolls) to viewDidLoad (which gets called once when the view is loaded). Here's the code:
- (void)myCollectionViewWasTapped:(UITapGestureRecognizer *)tap
{
CGPoint tapLocation = [tap locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:tapLocation];
if (indexPath)
{
MyCollectionViewCell *cell = (MyCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
CGRect mySubviewRectInCollectionViewCoorSys = [self.collectionView convertRect:cell.mySubview.frame fromView:cell];
if (CGRectContainsPoint(mySubviewRectInCollectionViewCoorSys, tapLocation))
{
// Yay! My subview was tapped!
}
}
}
- (void)viewDidLoad
{
// Invoke super
[super viewDidLoad];
// Add tap handler to collection view
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myCollectionViewWasTapped:)];
[self.collectionView addGestureRecognizer:tap];
}
Here's a rough, very simple outline of a possible design solution: you could subclass UICollectionViewCell and override its initialization methods to add the gesture recognizer to its subviews. Furthermore, if you don't want the cell to "know" about the gesture recognizer, you could create a protocol that the data source object would conform to. The cell object would call a "setup" protocol method at the appropriate time.
Hope this helps!

Resources