How can I drag a UICollectionViewCell from one UICollectionView to another UICollectionView? - ios

I am making an iPad application. On one page of this application, there is a UICollectionView on the left-hand side and another UICollectionView on the right hand side. Each UICollectionView is one column wide.
The functionality I desire is as follows:
Each UICollectionViewCell on the left hand side should be able to be dragged to the UICollectionView on the right hand side. If this is not possible, then at least a UICollectionViewCell should be able to be dragged out of the left UICollectionView and then I'll handle having it appear in the righthand UICollectionView.
Is this functionality possible? If so, how would I go about implementing it?

You would want to attach a long press gesture recognizer to the common superview of both collectionViews. The drag operation is triggered by the long-press, and the entire transaction is handled within that recognizer. Because the pan gesture is used for scrolling the collectionviews, you will run into problems in trying to use the pan recognizer.
The key thing is the gesture recognizer needs to be attached the COMMON superview, and all points and rectangles are converted to the coordinate system of the superview.
This isn't the exact code (this moves from a CV to another view) but the process would be similar (NOTE: I have tried to strip out some code that would be irrelevant to your app, so I could have messed something up in the process -- but the concept holds):
- (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;
}
}

There's no way to actually 'pass' a cell from a collection to the other, but you can do the following:
1) Once you detect that the user dragged a cell in the other collection, delete the cell from the first collection (let's call it Collection 1). You can maybe use a nice fade animation to make the cell disappear.
2) Add a cell to the second table with a nice animation (see the UICollectionView and UICollectionViewLayout methods and delegate methods for this).

Related

Resetting the Zoom on a UIPageViewController

I am building up a simple application that is made up of a UITableViewController with languages and when a specific cell is clicked, a UIPageViewController is brought up to represent the images for that selected language. The user can scroll through the images and everything works as desired. The next step was to build a zooming capability into the UIPageViewController so the user could zoom into the images with a pinch gesture.
I have achieved this with the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.leafletImages = [NSMutableArray arrayWithObjects:[[ImageModel alloc] initWithImageName:#"3facts-chinese-page1.jpg"], [[ImageModel alloc] initWithImageName:#"3facts-chinese-page2.jpg"], [[ImageModel alloc] initWithImageName:#"3facts-chinese-page3.jpg"], [[ImageModel alloc] initWithImageName:#"3facts-chinese-page4.jpg"], [[ImageModel alloc] initWithImageName:#"3facts-chinese-page5.jpg"], [[ImageModel alloc] initWithImageName:#"3facts-chinese-page6.jpg"], nil];
// Lots of code for the building up of the UIPageViewController
LeafletImageSizeViewController *imageViewController = [[LeafletImageSizeViewController alloc] init];
imageViewController.model = [_modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:imageViewController];
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
// Gesture
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchDetected:)];
[self.view addGestureRecognizer:pinchRecognizer];
pinchRecognizer.delegate=self;
}
The class creating the image and the size is:
- (void)useThreeFactsSize
CGRect insetFrame;
insetFrame = CGRectMake(310, 70, self.view.frame.size.width-615, self.view.frame.size.height-85);
_imageView = [[UIImageView alloc] initWithFrame:insetFrame];
[_imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[_imageView setImage:[UIImage imageNamed:_model.imageName]];
[[self view] addSubview:_imageView];
[self.view setBackgroundColor:[UIColor clearColor]];
}
The pinchDetection method is:
-(void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
CGFloat scale = pinchRecognizer.scale;
self.pageViewController.view.transform = CGAffineTransformScale(self.pageViewController.view.transform, scale, scale);
pinchRecognizer.scale = 1.0;
}
Now, I can zoom into the images of the UIPageViewController without any issues and it works really well.
What I want to do however is two things:
Not allow the image to be zoomed out beyond the original scale
Create a double tap gesture to bring the image back to it's original scale
With feature 1, the user can zoom into the image, but also completely zoom out of the image which shrinks the image and the UIPageViewController pageIndicators. There's no reason the user should be able to zoom out of the image, so I'd like to allow the user to zoom in to any scale, but not to zoom out beyond what the original size of the image on screen in the UIPageViewController.
With feature 2, I'd like to implement a gesture to double tap the screen and for the zoomed image to go back to it's original scale (like the Photos.app).
Update
With reference to the answer, I have updated the question to reflect how I'm going about doing the images. With point 2 and the double tap gesture, the following code almost works:
- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer *)recognizer
{
NSLog(#"Double Tap");
self.pageViewController.view.transform = CGAffineTransformIdentity;
}
What it's currently doing is if I zoom in with a pinch and pan around, and then double tap, it centres the image to the point of where I tapped, so sometimes the borders are being shown, etc, rather than making the image centre to where it's supposed to be.
For point 1:
if (pinchRecognizer.scale > 1) {
CGFloat scale = pinchRecognizer.scale;
self.pageViewController.view.transform = CGAffineTransformScale(self.pageViewController.view.transform, scale, scale);
pinchRecognizer.scale = 1.0;
}
If I have self.imageview, it doesn't work because it's nil and even if I make a call to the class setting the size, it's nil as well.
I suspect I have a number of things wrong with my code!
For reference, I have panning working with:
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [recognizer translationInView:recognizer.view];
[recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
else if(state==UIGestureRecognizerStateEnded){
UIView *imageView = recognizer.view;
UIView *container = imageView.superview;
CGFloat targetX = CGRectGetMinX(imageView.frame);
CGFloat targetY = CGRectGetMinY(imageView.frame);
if(targetX>0){
// targetX = 0;
}else if(CGRectGetMaxX(imageView.frame)<CGRectGetWidth(container.bounds)){
targetX = CGRectGetWidth(container.bounds)-CGRectGetWidth(imageView.frame);
}
if(targetY>0){
// targetY = 0;
}else if(CGRectGetMaxY(imageView.frame)<CGRectGetHeight(container.bounds)){
// targetY = CGRectGetHeight(container.bounds)-CGRectGetHeight(imageView.frame);
}
// imageView.frame = CGRectMake(targetX, targetY, CGRectGetWidth(imageView.frame), CGRectGetHeight(imageView.frame));
[UIView animateWithDuration:0.3 animations:^{
imageView.frame = CGRectMake(targetX, targetY, CGRectGetWidth(imageView.frame), CGRectGetHeight(imageView.frame));
}];
}
}
That's working very well at the moment, but there's definitely a conflict with everything else.
I'd really appreciate any guidance in the right direction on this.
1.) To not allow the image to be zoomed out beyond it's original scale you first just need to check if the scale you're about to set it to is greater than 1 or not. If it's less than one, you don't want to rescale your image as that would mean it gets smaller. So...
#IBAction func doPinch(sender: UIPinchGestureRecognizer) {
if sender.scale > 1 {
let transform = CGAffineTransformMakeScale(sender.scale, sender.scale)
imageView.transform = transform
}
}
2.) You seem to be changing the view's frame to try and change it's scale, but you never adjusted the view's frame yourself in the first place. You're adjusting the view's transform. That means in order to return it to it's original size you must remove whatever transform you put on it. To do this you put it back to it's identity. So...
#IBAction func doDoubleTap(sender: UITapGestureRecognizer) {
imageView.transform = CGAffineTransformIdentity
}
My code samples are in Swift but you should be able to adjust it to Objective-C yourself. Also, you seem to be adjusting the transform/scale of the entire page view itself. I would suggest you change the scale of only the image view. That makes more sense as that's actually what you're trying to zoom into.

Don't Move Table View Cells with a Long Press Gesture on specific position

I've completely implemented the UILongGesture in my App which exchanges the cell value by drag and drop. For now I've requirement that if I move first row with last row then first row should remain at first position means don't want change the position.
I've tried chunk of codes and wasted my time but couldn't get result. Below is my code.
- (IBAction)longPressGestureRecognized:(id)sender{
UILongPressGestureRecognizer *longGesture = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longGesture.state;
CGPoint location = [longGesture locationInView:self.tblTableView];
NSIndexPath *indexpath = [self.tblTableView indexPathForRowAtPoint:location];
static UIView *snapshotView = nil;
static NSIndexPath *sourceIndexPath = nil;
switch (state) {
case UIGestureRecognizerStateBegan:
if (indexpath) {
sourceIndexPath = indexpath;
UITableViewCell *cell = [self.tblTableView cellForRowAtIndexPath:indexpath];
snapshotView = [self customSnapshotFromView:cell];
__block CGPoint center = cell.center;
snapshotView.center = center;
snapshotView.alpha = 0.0;
[self.tblTableView addSubview:snapshotView];
[UIView animateWithDuration:0.25 animations:^{
center.y = location.y;
snapshotView.center = center;
snapshotView.transform = CGAffineTransformMakeScale(1.05, 1.05);
snapshotView.alpha = 0.98;
cell.alpha = 0.0;
} completion:^(BOOL finished) {
cell.hidden = YES;
}];
}
break;
case UIGestureRecognizerStateChanged: {
CGPoint center = snapshotView.center;
center.y = location.y;
snapshotView.center = center;
if (indexpath && ![NSIndexPath isEqual:sourceIndexPath]) {
[self.namesArray exchangeObjectAtIndex:indexpath.row withObjectAtIndex:sourceIndexPath.row];
[self.tblTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexpath];
sourceIndexPath = indexpath;
NSIndexPath *indexPathOfLastItem =[NSIndexPath indexPathForRow:([self.namesArray count] - 1) inSection:0];
NSLog(#"last :::: %#",indexPathOfLastItem);
if (indexpath==indexPathOfLastItem) {
[self.namesArray exchangeObjectAtIndex:indexPathOfLastItem.row withObjectAtIndex:sourceIndexPath.row];
[self.tblTableView moveRowAtIndexPath:indexPathOfLastItem toIndexPath:0];
UITableViewCell *cell = [self.tblTableView cellForRowAtIndexPath:sourceIndexPath];
cell.hidden = NO;
cell.alpha = 0.0;
}
}
break;
}
default: {
UITableViewCell *cell = [self.tblTableView cellForRowAtIndexPath:sourceIndexPath];
cell.hidden = NO;
cell.alpha = 0.0;
[UIView animateWithDuration:0.25 animations:^{
snapshotView.center = cell.center;
snapshotView.transform = CGAffineTransformIdentity;
snapshotView.alpha = 0.0;
cell.alpha = 1.0;
} completion:^(BOOL finished) {
sourceIndexPath = nil;
[snapshotView removeFromSuperview];
snapshotView = nil;
}];
break;
}
}
}
EDIT: What I have come across is the cell is not exchanging that's what I want but it is hidden. Here is the image: Image1 and Image2
First of all, I don't think you should do the row exchanges in UIGestureRecognizerStateChanged but instead in UIGestureRecognizerStateEnded. UIGestureRecognizerStateChanged is processed rapidly(many many times) while you are long pressing and moving your finger across the screen. So this causes the row exchange code to run many times which is not your intention I guess. UIGestureRecognizerStateBegan and UIGestureRecognizerStateEnded are run once per each longpress.
I would keep these following three lines of code in UIGestureRecognizerStateChanged and move the rest to UIGestureRecognizerStateEnded:
CGPoint center = snapshotView.center;
center.y = location.y;
snapshotView.center = center;
The UIGestureRecognizerStates are as follows:
UIGestureRecognizerStatePossible, // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan, // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged, // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded, // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled, // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed, // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
And instead of using switchs default:, I believe it will be better to cover more of these states in your state machine logic.
When your finger detaches from the screen during animation, iOS returns gesture.state = UIGestureRecognizerStatePossible.
So remember to handle that gesture!

Get position of UIButton in UITableViewCell

I have a button in a static UITableViewCell. What I want to do is place a UIView at the buttons location.
If I just get the buttons position, it will give me a position relative to the tableviews cell, not from the top of the tableViewController. What I want is get the buttons position from the top of the tableViewController.
For example: If the button is 8px away from the top of the current
cell, but 57px away from the top of the viewController, I want to
place the UIView at 57px.
Here's my code:
- (void)viewDidLoad {
CGPoint buttonPosition = [button convertPoint:CGPointZero fromView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
CGRect rect = [myTableView rectForRowAtIndexPath:indexPath];
NSLog(#"%f, %f", rect.origin.y, rect.origin.x);
self.infoView = [[UIView alloc] initWithFrame:CGRectMake(button.frame.origin.x, button.frame.origin.y, 220, 110)];
}
The output of the nslog is 0.
I would also like to update the UIView's position whenever the iphone goes to landscape mode.
CGPoint originalPoint = button.frame.origin;
CALayer *layer = self;
CGPoint point = originalPoint;
while (layer.superlayer)
{
point = [layer convertPoint:point toLayer:layer.superlayer];
layer = layer.superlayer;
}
//When you reach this code, you should have the position in the rootView

Reordering rotated uitableviewcell

I've got a uitableview that I've rotated 180 degrees but whose cells I've rotated back -180 degrees. The effect is that I am loading my data from the bottom of the table view. This works great. The problem comes when I try to reorder the cells. If I try to drag a cell its movement is inverted and jumps immediately to the top/bottom of the table view, depending on which direction I was dragging.
The easiest way to see this is by starting a new project using the master detail template with core data. In the OIMasterViewConroller.m file place
CGAffineTransform transform = CGAffineTransformMakeRotation(-3.14159);
self.tableView.transform = transform;
in the viewDidLoad method, and place
CGAffineTransform transform = CGAffineTransformMakeRotation(-3.14159);
cell.transform = transform;
in the configureCell method. Finally, change the canMoveRowAtIndexPath to return YES and implement a blank
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
Run the project and add a few items to the tableview with the + button. Then Click on the edit button and try to reorder.
I believe the problem lies with the cell since removing the rotation on the cell will stop this strange behavior, even though the table is upside down.
I thought about just rotating the text inside the cell, but then the delete buttons are upside down, as well as on the wrong side.
So, I ended up subclassing both the tableview and the cell used in the table view. Here is some of what I've done:
In the tableview subclass I overload the layoutsubviews:
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:#"UIShadowView"]) {
subview.hidden = YES;
}
}
}
This removes the drop shadow. If you don't the shadow shows at the top of the tableviewcells when they are being reordered.
For the tableviewcell subclass I changed its layoutSubviews like this:
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"]) {
CGRect newFrame = subview.frame;
newFrame.origin.x = 0;
subview.frame = newFrame;
subview.transform = CGAffineTransformMakeRotation(-M_PI);
}
else if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellEditControl"]) {
CGRect newFrame = subview.frame;
newFrame.origin.x = 320-newFrame.size.width;
subview.frame = newFrame;
subview.transform = CGAffineTransformMakeRotation(-M_PI);
}
else if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellReorderControl"]) {
CGRect newFrame = subview.frame;
newFrame.origin.x = 0;
subview.frame = newFrame;
}else if([NSStringFromClass([subview class]) isEqualToString:#"UIButton"]) {
CGRect newFrame = subview.frame;
newFrame.origin.x = 0;
subview.frame = newFrame;
subview.transform = CGAffineTransformMakeRotation(-M_PI);
}else{
subview.transform = CGAffineTransformMakeRotation(-M_PI);
}
}
}
This rotates and moves all the delete buttons/indicators ect. The important thing to note is that we DO NOT rotate the UITableViewCellReorderControll. Rotating this subview is what makes the reordering messed up.

Transferring touches to copy of UIImageView

I've been a long journey trying to drag and drop UIImageViews from a UITableView to another view.
I have everything working to a point. I subclass UIPanGestureRecognizer and attach this custom recognizer to all the UIImageViews I load into the table view.
I then override the recognizer's touchesMoved method to, among other things, add a temporary view onto the applications key window, then add a copy of the selected UIImageView onto this temporary view, as follows:
UIView *dragAndDropView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
dragAndDropView.backgroundColor = [UIColor whiteColor];
[[UIApplication sharedApplication].keyWindow addSubview: dragAndDropView];
ElementImageView * sourceImageView = (ElementImageView*)self.view;
ElementImageView *newImageView = [[ElementImageView alloc] initWithImage:sourceImageView.image];
newImageView.userInteractionEnabled = YES;
newImageView.frame = [self.view convertRect:sourceImageView.frame toView:dragAndDropView];
[dragAndDropView addSubview:newImageView];
As a result of this code, I do indeed get a copy of the source image view on top of my dragAndDropView (I can tell because I set the backgroundcolor of the dragAndDropView to white). And if I lift my finger and touch the copied image view again, I can of course drag it around the screen as I wish. But I can't find a way to seamlessly transfer the existing touch to the new object.
Any ideas?
I think you could do the following on your UIPanGestureRecognizer handler:
-(void) handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
// the initial position
initialPanPositionX = self.transform.tx;
initialPanPositionY = self.transform.ty;
// create a copy of the imageview
panImage = [[UIImageView alloc] initWithImage:imageView.image];
panImage.frame = imageView.frame;
[self.view addSubview:panImage];
}
else if (recognizer.state == UIGestureRecognizerStateChanged) {
if (!startedMoving) {
startedMoving = YES;
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.2f];
// an effect to increase a copy a little bit when start dragging
CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
panImage.bounds = CGRectApplyAffineTransform(panImage.bounds, zt);
}
[UIView commitAnimations];
}
// translation
CGPoint point = [recognizer translationInView:self];
panImage.transform = CGAffineTransformMakeTranslation(initialPanPositionX + point.x, initialPanPositionY + point.y);
}
else if (recognizer.state == UIGestureRecognizerStateEnded ) {
startedMoving = NO;
// drop the imageView if it's inside the droppable view
[panImage release], panImage = nil;
}
}
Hope it helps.

Resources