How to drag a UICollectionViewCell from one UICollectionView to another UICollectionView?

I am making an iPad application. On one page of this application on the left is a UICollectionView and another UICollectionView on the right. Each UICollectionView has a single column width.

The functionality I wish for is as follows: Each UICollectionViewCell on the left should be ported to a UICollectionView on the right. If this is not possible, then at least the UICollectionViewCell should be inferred from the left UICollectionView, and then I will process its appearance in the right UICollectionView.

Is it possible to use this function? If so, how do I implement it?

+7
source share
3 answers

There is no way to β€œtransfer” a cell from the collection to another, but you can do the following:

1) Once you find that the user has dragged a cell to another collection, remove the cell from the first collection (let it collect it 1). You can use beautiful fading animations to make the camera disappear.

2) Add a cell to the second table with good animation (see UICollectionView and UICollectionViewLayout methods and delegation methods for this).

+5
source

You would like to attach a long gesture recognizer when printing to the general list of both collections. The drag and drop operation starts with a long press, and the entire transaction is processed inside this recognizer. Since the pan gesture is used to scroll through the collection, you will encounter problems when trying to use the pan recognizer.

The key point is the gesture recognizer that must be attached by the COMMON superview, and all points and rectangles are converted to the supervisor's coordinate system.

This is not an exact code (it moves from CV to another view), but the process will be similar (NOTE: I tried to highlight some code that is not related to your application, so I could ruin something in this process - but concept is preserved):

- (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) // ie, 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; } } 
+6
source

Take a look at this article, I hope http://weydev.com/en/ will help you

In this project you can drag cells in two uicollectionview

0
source

All Articles