How to implement UITableView`s swipe to delete for UICollectionView

前端 未结 6 504
你的背包
你的背包 2020-12-08 10:34

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.

6条回答
  •  春和景丽
    2020-12-08 11:22

    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
        }
    }
    

提交回复
热议问题