• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 通过长按手势交换 UICollectionView 单元格位置

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:27:03 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在弄清楚我在这里做错了什么时遇到了一些麻烦。我的目标是能够拖动 CollectionViewCell 并让它用拖动的单元格替换目标单元格,同时让替换的单元格接管最初拖动的单元格上的位置。这甚至让我感到困惑!我可以拖动并替换单元格,但我的问题是,当我发生时,我会在集合中留下越来越多的空单元格..

我对 Objective-C 相当陌生,所以很可能我在这里遗漏了一些愚蠢的东西。任何帮助将不胜感激。希望我包含了足够的相关代码来帮助并链接图像以显示我的问题...

干杯李

Collection View Issue Image

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getPlayerPositions];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getPlayerPositions
{
    SQL_PlayerTracker *pt = [[SQL_PlayerTracker alloc]init];
    playerPositionsARR = [[pt getPositions:MD]mutableCopy];
    [CV_playerPositions reloadData];
}

-(NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section
{
    return [playerPositionsARR count];
}

-(UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath
{
    OBJ_Positions *s = [playerPositionsARR objectAtIndex:indexPath.row];

    CVC_PlayerPositions *pcell = (CVC_PlayerPositions *)[collectionView dequeueReusableCellWithReuseIdentifier"ositionCell" forIndexPath:indexPath];
    pcell.playerName.text = s.playerName;
    pcell.positionName.text = s.position;
    pcell.playerNumber.text = [NSString stringWithFormat"%@",s.playerNumber];

    if (pcell.selected) {
        pcell.backgroundColor = [UIColor greenColor];
    }

    return pcell;
}

- (void)moveItemAtIndexPathNSIndexPath *)indexPath toIndexPathNSIndexPath *)newIndexPath
{
    NSLog(@"MOVED INDEX %i TO %i",indexPath.row,newIndexPath.row);
    OBJ_Positions *oldPlayer = [playerPositionsARR objectAtIndex:newIndexPath.row];
    OBJ_Positions *newPlayer = [playerPositionsARR objectAtIndex:indexPath.row];

    //REMOVE TARGET POSITION FROM ARRAY
    [playerPositionsARR removeObjectAtIndex:newIndexPath.row];
    //INSERT NEW PLAYER POSITION IN TARGET POSITION
    [playerPositionsARR insertObject:newPlayer atIndex:newIndexPath.row];
    //INSERT OLD PLAYER IN MOVED PLAYERS POSITION
    [playerPositionsARR insertObjectldPlayer atIndex:indexPath.row];
    //REMOVE ADDED POSITION OBJECT FROM INSERTION
    [playerPositionsARR removeObjectAtIndex:indexPath.row +1];
    [CV_playerPositions reloadData];
}

-(void)collectionViewUICollectionView *)collectionView didSelectItemAtIndexPathNSIndexPath *)indexPath
{
    OBJ_Positions *p = [playerPositionsARR objectAtIndex:indexPath.row];
    NSLog(@"Selected Player IS %@",p.playerName);
}

#pragma mark - MOVE COLLECTION VIEW CELLS

-(BOOL)gestureRecognizerShouldBeginUIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

-(void)dragPlayerUILongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"State Began");
        CGPoint point = [sender locationInView:CV_playerPositions];
        originalCellIndex = [CV_playerPositions indexPathForItemAtPoint:point];
        if (originalCellIndex) {
            UICollectionViewCell *cell = [CV_playerPositions cellForItemAtIndexPathriginalCellIndex];

        beginView =  [cell.contentView viewWithTag:cell.tag];

            beginView.backgroundColor = [UIColor greenColor];
            [beginView removeFromSuperview];

            beginView.center = [CV_playerPositions convertPoint:point toView:self.view];
            originalStartPoint = beginView.center;
            [self.view addSubview:beginView];
           [self.view bringSubviewToFront:beginView];

            OBJ_Players *pd = [playerPositionsARR objectAtIndexriginalCellIndex.row];

            NSLog(@"SELECT PLAYER NAME IS - %@",pd.playerName);
            return;
        }
    }

    if (sender.state == UIGestureRecognizerStateChanged) {
        NSLog(@"State Changed");
        if (!beginView)
            return;

        CGPoint location = [sender locationInView:self.view];
        CGPoint translation;

        translation.x = location.x - originalStartPoint.x;
        translation.y = location.y - originalStartPoint.y;
        CGAffineTransform theTransform = beginView.transform;
        theTransform.tx = translation.x;
        theTransform.ty = translation.y;
        beginView.transform = theTransform;

        return;
    }

    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"STATE ENDED");

        if (beginView) {
            CGPoint epoint = [sender locationInView:CV_playerPositions];
            endCellIndex = [CV_playerPositions indexPathForItemAtPoint:epoint];
            if(endCellIndex)
            {
                OBJ_Positions *p = [playerPositionsARR objectAtIndex:endCellIndex.row];

                NSLog(@"OSITION IS - %@ AND PLAYER DRAGGED IS %@",p.position,p.playerName);
                [self moveItemAtIndexPathriginalCellIndex toIndexPath:endCellIndex];
                [beginView removeFromSuperview];

                return;

            }
        }
    }
}

- (IBAction)movingPlayer:(id)sender {
    [self dragPlayer:sender];
}



Best Answer-推荐答案


我相信这个链接就是你要找的。 https://github.com/lxcid/LXReorderableCollectionViewFlowLayout

它扩展了 UICollectionViewFlowLayout 以支持单元格的重新排序。类似于 iBook 中的长按和平移。

from the github readme

关于ios - 通过长按手势交换 UICollectionView 单元格位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18738182/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap