I’m currently working on a drag and drop functionality and luckily I found a good tutorial. The goal of this project is to drag and drop “items” from one table view to another with the help of the UILongPressGestureRecognizer. As I have learned from this, you have to get a “snapshot” of a cell you chose in able to get the feel of dragging something out of a table. And I think, below is the method that does this.
- (void)dragAndDropTableViewController:(DragAndDropTableVC *)ddtvc draggingGestureWillBegin:(UIGestureRecognizer *)gesture forCell:(UITableViewCell *)cell
{
// create a snapshot of the cell
NSLog(@"draggable delegate method is called");
UIGraphicsBeginImageContext(cell.contentView.bounds.size);
[cell.contentView.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *cellImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:cellImage];
self.dragAndDropView = [[UIView alloc] initWithFrame:cellImageView.frame];
[self.dragAndDropView addSubview:cellImageView];
[self.dragAndDropView setBackgroundColor:[UIColor blueColor]];
[self.dragAndDropView setCenter:[gesture locationInView:self.view.superview]];
[self.view.superview addSubview:self.dragAndDropView];
}
Basically, upon getting the cell’s “snapshot”, you have to add it as a subview of a UIView. The thing that bugs me out is this part.
[self.dragAndDropView setCenter:[gesture locationInView:self.view.superview]];
This may sound easy but what I would want to do is when the long press is done, the draggable cell should appear a bit higher than the original location of the selected cell.
Enlighten me please 🙂 Thanks!
Actually you have to convert center point with respect to your superview then you should use one of these methods
convertPoint: fromView:andconvertPoint: toView:and for more information see this link.I think it will be helpful to you.