gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:28:49 25 4
gpt4 key购买 nike

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

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

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

最佳答案

您可能希望将长按手势识别器附加到两个 collectionView 的公共(public)父 View 。拖动操作由长按触发,整个事务在该识别器中处理。因为平移手势用于滚动 Collection View ,所以您在尝试使用平移识别器时会遇到问题。

关键是手势识别器需要附加到COMMON superview,所有的点和矩形都转换到superview的坐标系。

这不是确切的代码(它从 CV 移动到另一个 View )但过程是相似的(注意:我试图删除一些与您的应用程序无关的代码,所以我可能搞砸了过程中发生了一些事情——但这个概念是成立的):

- (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;
}
}

关于ios - 如何将 UICollectionViewCell 从一个 UICollectionView 拖到另一个 UICollectionView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17237971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com