zoukankan      html  css  js  c++  java
  • 长按移动cell

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        MineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCell forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        cell.cellImage.image = [UIImage imageNamed:self.imagesArray[indexPath.row]];
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
        [cell addGestureRecognizer:longPress];
        return cell;
    }

    - (void)longPressGesture:(UILongPressGestureRecognizer *)sender{
        MineCollectionViewCell *cell = (MineCollectionViewCell *)sender.view;
        NSIndexPath *cellIndexPath = [_mineCollection indexPathForCell:cell];
        [_mineCollection bringSubviewToFront:cell];
        BOOL isChanged = NO;
        if (sender.state == UIGestureRecognizerStateBegan) {
            [self.cellAttributesArray removeAllObjects];
            for (int i = 0;i< self.imagesArray.count; i++) {
                [self.cellAttributesArray addObject:[_mineCollection layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]];
            }
            self.lastPressPoint = [sender locationInView:_mineCollection];
        }else if (sender.state == UIGestureRecognizerStateChanged){
            cell.center = [sender locationInView:_mineCollection];
            for (UICollectionViewLayoutAttributes *attributes in self.cellAttributesArray) {
                if (CGRectContainsPoint(attributes.frame, cell.center) && cellIndexPath != attributes.indexPath) {
                    isChanged = YES;
                    //对数组中存放的元素重新排序
                    NSString *imageStr = self.imagesArray[cellIndexPath.row];
                    [self.imagesArray removeObjectAtIndex:cellIndexPath.row];
                    [self.imagesArray insertObject:imageStr atIndex:attributes.indexPath.row];
                    [self.mineCollection moveItemAtIndexPath:cellIndexPath toIndexPath:attributes.indexPath];
                    
                    
                }
            }
            
        }else if (sender.state == UIGestureRecognizerStateEnded) {
            if (!isChanged) {
                cell.center = [_mineCollection layoutAttributesForItemAtIndexPath:cellIndexPath].center;
            }
            NSLog(@"排序后---%@",self.imagesArray);
        }
        
        
    }

  • 相关阅读:
    iOS添加Google语言识别功能
    转载:Ajax中get与post请求详解
    SSH:Spring+Spring MVC+hibernate整合开发笔记
    IDEA中创建maven web项目的详细部署步骤
    C++实现RTMP协议发送H.264编码及AAC编码的音视频
    C++ 勘误
    Postgresql ODBC驱动,用sqlserver添加dblink跨库访问postgresql数据库
    .Net NPOI 上传excel文件、提交后台获取excel里的数据
    .Net NPOI 根据excel模板导出excel、直接生成excel
    .Net 登陆的时候添加验证码
  • 原文地址:https://www.cnblogs.com/lidongq/p/5535633.html
Copyright © 2011-2022 走看看