Take the 2-minute tour ×

I just like to ask how can I implement the same behavior of UITableView`s swipe to delete in UICollectionView. I am trying to find a tutorial but I cannot find any.

Also, I am using PSTCollectionView wrapper to support iOS 5.

Thank you!

Edit: The swipe recognizer is already good. What I need now is the same functionality as UITableView's when cancelling the Delete mode, e.g. when user taps on a cell or on a blank space in the table view (that is, when user taps outside of the Delete button). UITapGestureRecognizer won't work, since it only detects taps on release of a touch. UITableView detects a touch on begin of the gesture (and not on release), and immediately cancels the Delete mode.

share|improve this question

4 Answers 4

You can try adding a UISwipeGestureRecognizer to each collection cell, like this:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath
    CollectionViewCell *cell = ...

    UISwipeGestureRecognizer* gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(userDidSwipe:)];
    [gestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
    [cell addGestureRecognizer:gestureRecognizer];

followed by:

- (void)userDidSwipe:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        //handle the gesture appropriately
share|improve this answer
Thank you for your response! However, UITableView`s swipe to delete allow user to cancel the delete upon touch of the screen. I cannot seem to implement that properly. Any suggestions? –  MiuMiu Jan 11 '13 at 3:49
The same way: create a UITapGestureRecognizer in the "handle the gesture appropriately" part. –  Jacek Lampart Jan 11 '13 at 4:44
Please see edited question. Thanks! –  MiuMiu Jan 14 '13 at 23:50

I followed a similar approach to @JacekLampart, but decided to add the UISwipeGestureRecognizer in the UICollectionViewCell's awakeFromNib function so it is only added once.


- (void)awakeFromNib {
    UISwipeGestureRecognizer* swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToDeleteGesture:)];
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:swipeGestureRecognizer];

- (void)swipeToDeleteGesture:(UISwipeGestureRecognizer *)swipeGestureRecognizer {
    if (swipeGestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // update cell to display delete functionality

As for exiting delete mode, I created a custom UIGestureRecognizer with an NSArray of UIViews. I borrowed the idea from @iMS from this question: UITapGestureRecognizer - make it work on touch down, not touch up?

On touchesBegan, if the touch point isn't within any of the UIViews, the gesture succeeds and delete mode is exited.

In this way, I am able to pass the delete button within the cell (and any other views) to the UIGestureRecognizer and, if the touch point is within the button's frame, delete mode will not exit.


#import <UIKit/UIKit.h>

@interface TouchDownExcludingViewsGestureRecognizer : UIGestureRecognizer

@property (nonatomic) NSArray *excludeViews;



#import "TouchDownExcludingViewsGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@implementation TouchDownExcludingViewsGestureRecognizer

    if (self.state == UIGestureRecognizerStatePossible) {
        BOOL touchHandled = NO;
        for (UIView *view in self.excludeViews) {
            CGPoint touchLocation = [[touches anyObject] locationInView:view];
            if (CGRectContainsPoint(view.bounds, touchLocation)) {
                touchHandled = YES;

        self.state = (touchHandled ? UIGestureRecognizerStateFailed : UIGestureRecognizerStateRecognized);

    self.state = UIGestureRecognizerStateFailed;

    self.state = UIGestureRecognizerStateFailed;


Implementation (in the UIViewController containing UICollectionView):

#import "TouchDownExcludingViewsGestureRecognizer.h"

TouchDownExcludingViewsGestureRecognizer *touchDownGestureRecognizer = [[TouchDownExcludingViewsGestureRecognizer alloc] initWithTarget:self action:@selector(exitDeleteMode:)];
touchDownGestureRecognizer.excludeViews = @[self.cellInDeleteMode.deleteButton];
[self.view addGestureRecognizer:touchDownGestureRecognizer];

- (void)exitDeleteMode:(TouchDownExcludingViewsGestureRecognizer *)touchDownGestureRecognizer {
    // exit delete mode and disable or remove TouchDownExcludingViewsGestureRecognizer
share|improve this answer

In the Collection View Programming Guide for iOS, in the section Incorporating Gesture Support, the docs read:

You should always attach your gesture recognizers to the collection view itself and not to a specific cell or view.

So, I think it's not a good practice to add recognizers to UICollectionViewCell.

share|improve this answer

Running across this question a little late, but here is a library I've used to do exactly what you're looking for: https://github.com/Raizlabs/RZUtils/tree/master/RZUtils/Components/RZCollectionTableView

Hope that helps anyone else who comes across this problem in the future!

share|improve this answer

Your Answer


