How can I catch the gestures over UICollectionView? - ios

I have a UICollectionView and a custom UICollectionViewCell
I want to be able to catch the UICollectionView gestures as a UIGestureRecognizerDelegate, actually I want to handle some gestures collisions by using this delegate's method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
How can I catch the UICollectionView's UIGestureRecognizerDelegate?

UICollectionView does listen for taps but not by using a UIGestureRecognizer.
But you could add your own UIGestureRecognizer for the type your interested in (like UITapGestureRecognizer for instance) to the UICollectionView, set the delegate on it and in gestureRecognizerShouldBegin: return YES or NO depending on whether you want the UICollectionView to do it's thing or not, i.e. returning NO would cancel your gesture and allow the collection view to handle the touches.
Or set delayTouchesBegan to YES if you just want your gestures to take priority over the collection view touch handling.
More info is here Collection View Programming Guide

Related

Why does UICollectionView not scroll with UIButtons inside it?

Given the following scenario:
There is a UICollectionView containing UICollectionViewCells (naturally). Each UICollectionViewCell contains a UIButton with a target action associated with it.
Problem:
The UICollectionView does not scroll upon swipe/drag. The UIButtons instead intercept touches, and make it difficult to drag the UICollectionView.
Additionally:
The UIButtons are set to UIControlEventTouchUpInside
The UICollectionView is set to canCancelTouches = YES
**Why are the UIButtons inside UICollectionViewCells preventing the UICollectionView from responding to drag/swipe gestures? **
The reason that the UICollectionView doesn't scroll properly is because the UIButtons are intercepting and rerouting the responder chain.
Solution:
Remove the UIButtons from the UICollectionViewCell.
Instead use the delegate method for UICollectionViewDelegate :
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Explanation:
When we added a UIButton to the UICollectionViewCell, we were thinking that the best way to capture a tap was to add a button to the cell. However, by adding the UIButton, we broke the responder chain.
We don't need the button inside the UICollectionViewCell, because the UICollectionViewCell already detects tap events with it's delegate method :
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Use the provided method to detect taps on a collection cell, and replace the UIButtons with UIImageView-s or similar.
We don't need the event handing of a button when working with collection cells.

UIScrollView Vertical Scrolling when touching inside a subview

I have implemented a scrollview, which holds multiple copies of a custom view. The custom views contain a UIPanGestureRecoginixer that I use to swipe them to the left (to delete them). When I attempt to scroll vertically, scrolling doesn't occur if I'm touching inside of the CustomView. However if I set up the subViews and the scrollviews content size so that it scrolls horizontally this issue doesn't occur. How can I force the scrollview to scroll horizontally even if I'm touching inside of a subview?
Try to set the UIPanGestureRecoginzer's delegate and implement
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
It requires you to return a BOOL value. When you return YES, the otherGestureRecognizer will be recognized simultaneous.

How can i hide/show images while scrolling in UITableView?

I'm new to iOS programming and i want to learn something about UITableView.
I have some images in UITableViewCell. I want to hide my images when scrolling start then i want to show them when scrolling stop.
How can i do that ?
Is there a method or something else about that ?
UITableView inherits from UIScrollView, thus you can in your tableView delegate receive
UIScrollViewDelegate callbacks.
Implement the delegate method
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
which tells the delegate when the scroll view is about to start scrolling the content.
and
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
which tells the delegate when the user finishes scrolling the content.
Read the ScrollViewDelegate Reference.
It is not a big deal you simply need to implement scrollview delegated methodes to know when scrolling stops ,Then set a bool value and reload the table and on basis of bool value you can show and hide the images.When scrolling starts set bool to NO.

How to add GestureRecognizer for UITableView, but only on empty place?

I want to add tap - hide table + some actions - named cancelTap
I have an UITableView and i want to add cancelTap for it, but only at empty place.
Table height is 200.
Cells fill only a half of table, and on other half (half without cells) i need to add addGestureRecognizer.
If i'm add GestureRecognizer to Table - method DidSelectCellForRowAtIndexPath does not respond.
Is it any solutions?
I just add tap to the tableView and in the delegate of the Tap set method -
(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return _searchDisplayController.contentSize.height <= [touch locationInView:_searchDisplayController].y;
}
in this method i deside do i need to apply tap or i ned to ignore it.

UIPanGestureRecognizer on UITableViewCell overrides UITableView's scroll view gesture recognizer

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?

Resources