如何将UICollectionViewCell从一个UICollectionView拖到另一个UICollectionView?

eri*_*ikt 7 ipad ios ios6 uicollectionview uicollectionviewcell

我正在制作iPad应用程序.在这个应用程序的一个页面上,左侧有一个UICollectionView,右侧有另一个UICollectionView.每个UICollectionView都是一列宽.

我想要的功能如下:左侧的每个UICollectionViewCell应该能够被拖动到右侧的UICollectionView.如果这是不可能的,那么至少应该能够从左侧UICollectionView中拖出一个UICollectionViewCell,然后我将处理它出现在右侧UICollectionView中.

这个功能可以吗?如果是这样,我将如何实施呢?

Reg*_*ion 6

您可能希望将长按手势识别器附加到两个collectionView的公共超级视图.拖动操作由长按触发,整个事务在该识别器内处理.由于平移手势用于滚动集合视图,因此在尝试使用平移识别器时会遇到问题.

关键是手势识别器需要连接到COMMON superview,所有点和矩形都转换为superview的坐标系.

这不是确切的代码(这从CV转移到另一个视图)但过程类似(注意:我试图删除一些与您的应用无关的代码,所以我可能搞砸了一些东西过程 - 但概念成立):

- (void) processLongPress:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateChanged)
{
    if (!dragView)
        return;
    CGPoint location = [sender locationInView:self.view];
    CGPoint translation;
    translation.x = location.x - dragViewStartLocation.x;
    translation.y = location.y - dragViewStartLocation.y;
    CGAffineTransform theTransform = dragView.transform;
    theTransform.tx = translation.x;
    theTransform.ty = translation.y;
    dragView.transform = theTransform;
    [self.view bringSubviewToFront:dragView];
    return;
}   

if (sender.state == UIGestureRecognizerStateBegan)
{
    //  if point gives a valid collectionView indexPath we are doing a long press on a picture item to begin a drag
    //  & drop operation.
    CGPoint point = [sender locationInView:collectionView];
    dragViewIndexPath = [collectionView indexPathForItemAtPoint:point];
    if (dragViewIndexPath)  // i.e., selected item in collection view.
    {
        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:dragViewIndexPath];
        dragView = [cell.contentView viewWithTag:cell.tag];
        [dragView removeFromSuperview];
        [self.view addSubview:dragView];
        dragView.center = [collectionView convertPoint:point toView:self.view];
        dragViewStartLocation = dragView.center;
        [self.view bringSubviewToFront:dragView];
    }
    return;
}

if (sender.state == UIGestureRecognizerStateEnded)
{
    if (dragView)
    {
        dragView.center = CGPointMake(dragView.center.x + dragView.transform.tx, dragView.center.y + dragView.transform.ty);
        CGAffineTransform theTransform = dragView.transform;
        theTransform.tx = 0.0f;
        theTransform.ty = 0.0f;
        UIView *dropTarget = [self mapDisplayModeToReceiverView];   // get drop target

        CGRect convertedTargetFrame = [self.view convertRect:dropTarget.frame fromView:dropTarget.superview];
        if (CGRectContainsPoint(convertedTargetFrame, dragView.center)) // if so, then drop it.
        {
            ImageWithAttachedLabel *i = (ImageWithAttachedLabel *) dragView;
            [speakStrings addObject:[i.labelText stringByAppendingString:@". "]];
            UserData *uData = (UserData *)i.userDataObject;
            UIImage *image = [[UIImage alloc] initWithData:uData.image];
            CGRect newFrame = CGRectMake(0.0f, 0.0f, 140.0f, 140.0f);
            ImageWithAttachedLabel *newImage = [[ImageWithAttachedLabel alloc] initWithFrame:newFrame withImage:image withLabel:uData.itemName];
            newImage.tag = RECEIVERVIEW_MAGIC_NUMBER;
            [self.view addSubview:newImage];
            newImage.center = [receiverView convertPoint:dropTarget.center toView:self.view];
            [UIView animateWithDuration:0.35f animations:^{ newImage.transform = CGAffineTransformMakeScale(1.15f, 1.15f); newImage.transform = CGAffineTransformIdentity; }
                             completion:^(BOOL finished) {  if (finished)
                                                            {
                                                                [newImage removeFromSuperview];
                                                                newImage.frame = newFrame;
                                                                [dropTarget addSubview:newImage];
                                                                [dragView removeFromSuperview];
                                                                dragView=nil; }
                                                            }];

        }
        else
        {
            [dragView removeFromSuperview];
            dragView = nil;
        }

        [self reloadData];
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)


Gia*_*one 5

实际上没有办法将一个单元格从一个集合"传递"到另一个集合,但您可以执行以下操作:

1)一旦检测到用户拖动了另一个集合中的单元格,就从第一个集合中删除单元格(让我们称之为集合1).您可以使用漂亮的淡入淡出动画来使细胞消失.

2)使用漂亮的动画将单元格添加到第二个表(请参阅UICollectionView和UICollectionViewLayout方法以及委托方法).