How to recognize gesture from custom view in VC - ios

I have tableVC, it contains from cells with customViews.
That customViews contains from 3 imageViews, so then I click on imageView, it makes some action. To achieve it I added UITapGestureRecognizers to imageView.
Now I need to be notified then this cells are tapped in my tableVC. I get this events in didSelectRowAtIndexPath, but only if I do not touch any of imageViews with gestures.
How can I receive tap notification in my tableVC about touching my cell completely ?

If you want to get cell that is tapped - didSelectRowAtIndexPath is way to go. If you want to get the tapped image inside that cell - implement delegate for that ( https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html ) and do the corresponding actions. I see that you mentioned didSelectRowAtIndexPath, but I am not sure what was your problem.
Also I didn't put any code since I don't know if you are using swift or objective-c.

Related

iOS didSelectItemAtIndexPath doesn't get called if a tap gesture recognizer is added to its background view

I have a UIView A, and I added a UICollectionView B to A as a subView, so now view A is B's background view. Here didSelectItemAtIndexPath will get called normally at this time. But if I add a tap gesture recognizer to View A, then didSelectItemAtIndexPath won't be called. The code is simple like below
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap2)];
[viewA addGestureRecognizer:tap2];
How can I make didSelectItemAtIndexPath get called for this situation? My expected behavior is both tapping cell and tapping background methods get called normally and separately as they have different tap behaviors.
From Apple's documentation:
You should always attach your gesture recognizers to the collection view itself—not to a specific cell or view. The UICollectionView class is a descendant of UIScrollView, so attaching your gesture recognizers to the collection view is less likely to interfere with the other gestures that must be tracked. In addition, because the collection view has access to your data source and your layout object, you still have access to all the information you need to manipulate cells and views appropriately.
https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/IncorporatingGestureSupport/IncorporatingGestureSupport.html
I have done this and it works very well. Generally I would call indexPathForItemAtPoint: on the collectionView to get the indexPath and then get the cell from cellForItemAtIndexPath. In your case you can check to see that indexPathForItemAtPoint returns nil (ie the user is clicking on an area between the cells) and then do your code for that. You can also use UIView convertRect methods to figure out exactly where the touch it.

peek and pop for specific image from tableview cell

I have a table that displays different images (usually between 1 and 4 per cell). The images are programmatically added and aligned via auto layout.
I'm trying to implement the force touch method peek and pop, so that a user can push down on any of the images and the peek and pop will work.
I tried several different things to get the specific image, but none seem to be working. I added a tap gesture recognizer to each of the images in the cell that sends the index of the picture to the tableviewcontroller via a delegate, but the force touch method was called before the tap gesture was. I also tried creating an nsinteger property for the cell that gets set each time any of the cells get the touched. But this also doesn't get called before the force touch method.
How should this be implemented?
Peek and pop is merely a way of previewing a view controller transition. So think about how you would implement a transition where you tap on the cell and present the image in a presented view controller. Now configure that transition in the storyboard. Now check the Peek & Pop checkbox!

Detecting Cell Selection from within Another Cell

I have a UICollectionView. Its cell has a UICollectionView inside of it.
I can detect when the cell containing the collectionView is tapped (via didSelectItemAtIndexPath). However, I cannot detect when the cells of the collectionView inside the cell (as depicted by the individual sun icons) are tapped.
User-interaction is enabled on everything. And I've tried overriding the hitTest of the collectionView as described here.
Update: I tried this again in a simple test project and it worked fine. However in my current project for some reason the inner-most nested collectionView (with the sun icons) does not appear in the view debugger.
Notice I made the collectionView background blue and its cells pink. And they do not appear when view debugged, although they appear in the running app?
I've done this before... but it's going to be a little hard to explain.
The short answer is TapGestures.
So I had the tableView through storyboard drawn. The collection view was added programatically.
For TableView cellforRow, I called UITableViewCell subclass. Which I passed the rootView as a delegate. Which called a CollectionViewClass. This is where the actual items for the cell are being drawn and added to the collection view. At this point I have the rootdelegate. For each item i add a tapGesture.
let tap = UITapGestureRecognizer(target: self, action: #selector(viewTapped))
So each item now has a tap gesture inside it. Now in my view that was drawing i can call the rootview with which cell was tapped.
func viewTapped() {
tapDelegate?.milestoneTapped(passID) //tapDelegate = RootView
}
I hope this helps.
Once I changed from using XIBs for my cells to using regular cells from storyboard this problem did not occur.

Get indexPath of UICollectionViewCell

I have a custom CollectionViewCell class, I have then generated an amount of these inside of a CollectionView. I have then also added a modal segue when the ViewCells are touched. How would I get the id (or index path) of which cell was pressed when the modal segue is activated.
I saw on here that someone suggested I add into the ViewCell.m file the method
-(void)collectionView: (UICollectionView*)collectionView didSelectItemAtIndexPath: (NSIndexPath*)indexPath{
//get indexpath variable in here
}
But this method does not seem to be getting called. Can anyone help me with what method would need to be get called for when a CollectionViewCell has a modal segue to get the indexpath.
Thanks,
If the views you have added to your cells are handling the touch by firing a segue, then the collectionViewCell never has a chance to receive the touch.
If you want the collectionViewCell to handle it, make the subviews of your cell purely decorative by disabling their behavior to fire the segue. (for example, if they are buttons and "touchUpInside" fires the segue, then change them into views with no touch handling at all.).
Then the touch will be passed up through the view hierarchy until the CollectionViewCell handles it, which it does with the collectionView:didSelectItemAtIndexPath: method.
The method is a callback of the delegate class of the collection view object:
-(void)collectionView: (UICollectionView*)collectionView didSelectItemAtIndexPath: (NSIndexPath*)indexPath
So you should implement on the .m file of the delegate class of you collection view.

Push Detail View from UITableView only on a specific UIImage in the cell?

I've got a UITableView which pushes a detail view when I tap any of its cells. I'd like that segue to only occur when tapping a specific part of the cell, a UIImageView at its left side. Tapping the rest of the cell should trigger a UITapGestureRecognizer which triggers a different method.
How can I override the default tap on the UITableViewCell, so that simply tapping anywhere on the cell doesn't trigger the segue to the detail view? I still want that transition, just only triggered by the UIImageView.
I think you could achieve that by connecting the segue with the image in the prototype cell (Never tried that myself) and not with the table view.
If you don't have a prototype cell then you should programmatically invoke an IBAction type of method with the image views. Well, Image Views cannot do that. Simply abuse a UIButton of the same size, custom style and assign the image to that button. Works fine.
(It does not need to be an -(IBAction) type of method, but it does not harm and doing so ensures that everything works fine.)
Within that action method invoke the segue programmatically. The segue needs to have an ID for that which is unique within the storyboard.

Resources