我正在制作iPad应用程序。在这个应用程序的一个页面上,左侧有一个UICollectionView,右侧有另一个UICollectionView。每个UICollectionView都是一列宽。
我想要的功能如下: 左侧的每个UICollectionViewCell应该能够被拖动到右侧的UICollectionView。如果这是不可能的,那么至少应该能够从左UICollectionView中拖出一个UICollectionViewCell,然后我将处理它出现在右边的UICollectionView中。
这项功能可行吗?如果是这样,我将如何实施呢?
答案 0 :(得分:6)
您可能希望将长按手势识别器附加到两个collectionView的公共超级视图中。拖动操作由长按触发,整个事务在该识别器内处理。由于平移手势用于滚动集合视图,因此在尝试使用平移识别器时会遇到问题。
关键是手势识别器需要连接到COMMON超视图,所有点和矩形都转换为超视图的坐标系。
这不是确切的代码(这从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;
}
}
答案 1 :(得分:5)
没有办法真正将一个单元格从一个集合“传递”到另一个集合,但您可以执行以下操作:
1)一旦检测到用户拖动了另一个集合中的单元格,就从第一个集合中删除单元格(让我们称之为集合1)。您可以使用漂亮的淡入淡出动画使单元格消失。
2)使用一个漂亮的动画向第二个表添加一个单元格(请参阅UICollectionView和UICollectionViewLayout方法以及委托方法)。
答案 2 :(得分:0)
看看这篇文章,希望对你有所帮助 http://weydev.com/en/
在这个项目中,您可以在两个uicollectionview
中拖放单元格