UICollectionView添加长按手势,长按选中某一个item

很多时候,我们都需要在项目中添加长按手势,比如UICollectionView中,我们长按对某一个item进行删除,那么这时,我们就需要在集合试图中添加长按的手势,手势的添加是简单的,但是添加过手势之后,我们怎么区分我们长按选中的是哪一个item呢

首先,我们先来看看我们是如何添加长按手势的

1.创建集合试图,这个就比较简单了.创建完集合试图,我们在集合试图上面添加长按的手势

UIGestureRecognizerDelegate 先遵从协议

longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
    longPressGr.minimumPressDuration = 1.0;
    longPressGr.delegate = self;
    longPressGr.delaysTouchesBegan = YES;
    [_myCollectionView addGestureRecognizer:longPressGr];

2.我们在longpressToDo里面添加方法

-(void)longPressToDo:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    CGPoint p = [gestureRecognizer locationInView:self.collectionView];

    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");            
    } else {
        // get the cell at indexPath (the one you long pressed)
        UICollectionViewCell* cell =
        [self.collectionView cellForItemAtIndexPath:indexPath];
        // do stuff with the cell
    }
}

在else里面我们根据indexpath对不同的item进行区分,这样是不是很简单

 

转载于:https://www.cnblogs.com/nsjelly/p/4629584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在一个 UICollectionView 中实现两个小的 item一个大的 item 的布局,可以利用 UICollectionViewDelegateFlowLayout 协议的方法进行实现。 具体思路如下: 1. 首先确定一个大的 item 和两个小的 item 的大小,可以使用两个 CGFloat 类型的常量来表示。 2. 然后,在 collectionView(_:layout:sizeForItemAt:) 方法中,根据每个 item 所在的 indexPath,判断当前的 item 是大的 item 还是小的 item,并返回对应的大小。可以使用三目运算符或者 switch 语句来实现。 3. 最后,在 collectionView(_:layout:insetForSectionAt:) 方法中,设置 section 的内边距,使得大的 item 和小的 item 的排列能够正确地对齐。 下面是一个示例代码: ```swift class MyViewController: UIViewController { let bigItemSize: CGFloat = 200 let smallItemSize: CGFloat = 100 let itemSpacing: CGFloat = 10 let sectionInset: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) // ... 其他代码 ... } extension MyViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { if indexPath.item % 3 == 0 { // 大的 item return CGSize(width: bigItemSize, height: bigItemSize) } else { // 小的 item return CGSize(width: smallItemSize, height: smallItemSize) } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return sectionInset } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return itemSpacing } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return itemSpacing } } ``` 在上面的例子中,我们假设每行有三个 item,其中每个大的 item 占据一行,每个小的 item 占据一列。因此,在 minimumLineSpacingForSectionAt 和 minimumInteritemSpacingForSectionAt 方法中,我们都返回了 itemSpacing,即 item 之间的间距。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值