7

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

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

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

4

2 回答 2

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;
    }
}
于 2013-06-21T15:06:22.257 回答
5

没有办法真正将一个单元格从一个集合“传递”到另一个集合,但您可以执行以下操作:

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

2) 用漂亮的动画向第二个表格添加一个单元格(参见 UICollectionView 和 UICollectionViewLayout 方法和委托方法)。

于 2013-06-21T14:49:16.537 回答