Ios 如何将UICollectionViewCell从一个UICollectionView拖动到另一个UICollectionView?
我正在制作一个iPad应用程序。在此应用程序的一个页面上,左侧有一个UICollectionView,右侧有另一个UICollectionView。每个UICollectionView都有一列宽 我想要的功能如下: 左侧的每个UICollectionViewCell都应该能够拖动到右侧的UICollectionView。如果这是不可能的,那么至少应该能够从左侧UICollectionView中拖出一个UICollectionViewCell,然后我将处理它在右侧UICollectionView中的显示Ios 如何将UICollectionViewCell从一个UICollectionView拖动到另一个UICollectionView?,ios,ipad,ios6,uicollectionview,uicollectionviewcell,Ios,Ipad,Ios6,Uicollectionview,Uicollectionviewcell,我正在制作一个iPad应用程序。在此应用程序的一个页面上,左侧有一个UICollectionView,右侧有另一个UICollectionView。每个UICollectionView都有一列宽 我想要的功能如下: 左侧的每个UICollectionViewCell都应该能够拖动到右侧的UICollectionView。如果这是不可能的,那么至少应该能够从左侧UICollectionView中拖出一个UICollectionViewCell,然后我将处理它在右侧UICollectionView中
这个功能可能吗?如果是这样,我将如何实现它?实际上没有办法将单元格从一个集合“传递”到另一个集合,但您可以执行以下操作: 1) 一旦检测到用户拖动了另一个集合中的单元格,请从第一个集合中删除该单元格(我们称之为集合1)。您可以使用一个漂亮的淡入淡出动画使细胞消失
2) 将一个单元格添加到第二个表格中,并带有一个漂亮的动画(请参见UICollectionView和UICollectionViewLayout方法和委派方法)。您需要将长按手势识别器附加到两个CollectionView的公共superview。拖动操作由长按触发,整个事务在该识别器中处理。由于平移手势用于滚动CollectionView,因此在尝试使用平移识别器时会遇到问题 关键是手势识别器需要附加到通用superview,所有点和矩形都转换为superview的坐标系 这不是确切的代码(这会从一个简历移动到另一个视图),但过程会类似(注意:我曾试图去掉一些与你的应用程序无关的代码,因此我可能会在过程中弄乱一些东西,但这个概念是成立的):
谢谢,这很有道理。我如何实现可拖动单元格?我是否需要使用类似的工具,或者是否有更简单的资源可供您指点?谢谢。您可以使用此lxrorederablecollectionviewflowlayout,它还允许您使用手势对单元格进行排序,或者在手势开始时在触摸点获取单元格(UICollectionView有一个方法),然后在用户拖动单元格时相应地移动它,只需向您的集合中添加一个UIPangestureRecognitor。当手势结束或取消时,您可以检查手机的位置并采取相应的行动。再次感谢,第二个选项听起来正是我想要的,而且也很容易实现。不客气。因为我注意到你对这个社区很陌生,我只想让你注意到,你可以“接受”一个答案,随着你声誉的提高,你也会得到一些特权。你首先会得到的一个答案是“向上投票”好的答案或“向下投票”坏的答案。如果你喜欢,请随意接受我的回答;)您好@regularExpression,您的代码中的dragView是什么?您好,dragView只是指向正在拖动的UIView的指针。它被设置为在手势识别器“StateStarted”代码中拖动的视图:dragView=[cell.contentView viewWithTag:cell.tag]在这种情况下,视图是集合视图中单元格的内容,但实际上可以是任何视图。
- (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;
}
}