Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gestures on a UICollectionViewCell; what to do with the result?

I'm currently working on a project for an iPad app. The main screen is a UICollectionView with AlbumCell's, a subclass of UICollectionViewCell. Now I wanted to add an UILongPressGestureRecognizer to pop up an UIActionSheet.

First I tried that in the UICollectionViewController, but I figured that that isn't the right place to add those. So my best guess is adding the gesture in the AlbumCell class? Then probably adding itself as delegate, so it catches it's own gestures.

Is this a good approach so far?

After I catch the gesture, I should show the UIActionSheet. Now I open it in the UICollectionViewController when the user selects a cell while in editing mode. But should I call a method on the UICollectionViewController to open it, like I do now? Or should the cell handle it's own UIActionSheet?

Eventually I got to let the UICollectionViewController what to do, might be to let him open the UIActionSheet, or handle accordingly to the result of it. How should the AlbumCell "communicate" with it?

It's something I've been struggling with multiple times, not just in this use case. Is the approach close, or should I handle those actions totally different?

like image 247
Guido Hendriks Avatar asked Sep 21 '12 01:09

Guido Hendriks


People also ask

What is CollectionView in swift?

CollectionView is an object that presents the ordered collection of data items in the customizable layouts. It shows the data in the form of a grid layout. A collectionview is an instance of the UICollectionView class, which inherits the UIScrollView, which will be covered later in this tutorial.

How does CollectionView work?

A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually. Collection views are a collaboration between many different objects, including: Cells. A cell provides the visual representation for each piece of your content.

How do you use UILongPressGestureRecognizer?

UILongPressGestureRecognizer is a concrete subclass of UIGestureRecognizer . The user must press one or more fingers on a view and hold them there for a minimum period of time before the action triggers.


1 Answers

You can adopt the same method of using UILongPressGestureRecognizer on UITableView.

Basically, set up one recognizer. In the selector, use the indexPathForItemAtPoint: to find out what index path the user was pressing on. Finally, if the user is pressing on a cell (and not pressing on the space in between cells), present the UIActionSheet however you like.

like image 105
Ash Furrow Avatar answered Nov 24 '22 04:11

Ash Furrow