简体   繁体   中英

Drag & drop between view controllers (contained in the same view controller)

I have a view controller, which is shown in the detail view of a split view and which consists of a TableView and a Core Plot chart view as shown below:

我的视图控制器

I would like to be able to drag & drop individual TableViewCells to the Core Plot view.

What would be the best way to do this, since gesture recognizers can be bound to one view only?

How would it be possible to animate the drag & drop move?

Additional question regarding view controller containment

How could I implement drag and drop from one view controller to a different view controller, if both are contained in the same view controller container (as in iOS 5).

Thank you!

Here is some code I use to drag a row from the tableview on the left panel of a split view to the right panel details screen. This seems similar to your issue, though admittedly not identical. Anyway, I personally trigger the drag and drop from a long press:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.tableView addGestureRecognizer:longPress];

And then my handler does the following:

- (IBAction)longPress:(UIGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan)
    {        
        // figure out which item in the table was selected

        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[sender locationInView:self.tableView]];
        if (!indexPath)
        {
            _inDrag = NO;
            return;
        }

        _inDrag = YES;

        // get the text of the item to be dragged

        NSString *text = [NSString stringWithString:[[_objects objectAtIndex:indexPath.row] description]];

        // create item to be dragged, in this example, just a simple UILabel

        UIView *splitView = self.splitViewController.view;
        CGPoint point = [sender locationInView:splitView];
        UIFont *font = [UIFont systemFontOfSize:12];
        CGSize size = [text sizeWithFont:font];
        CGRect frame = CGRectMake(point.x - (size.width / 2.0), point.y - (size.height / 2.0), size.width, size.height);
        _draggedView = [[UILabel alloc] initWithFrame:frame];
        [_draggedView setFont:font];
        [_draggedView setText:text];
        [_draggedView setBackgroundColor:[UIColor clearColor]];

        // now add the item to the view

        [splitView addSubview:_draggedView];
    }
    else if (sender.state == UIGestureRecognizerStateChanged && _inDrag) 
    {
        // we dragged it, so let's update the coordinates of the dragged view

        UIView *splitView = self.splitViewController.view;
        CGPoint point = [sender locationInView:splitView];
        _draggedView.center = point;
    }
    else if (sender.state == UIGestureRecognizerStateEnded && _inDrag)
    {
        // we dropped, so remove it from the view

        [_draggedView removeFromSuperview];

        // and let's figure out where we dropped it

        UIView *detailView = self.detailViewController.view;
        CGPoint point = [sender locationInView:detailView];

        UIAlertView *alert;
        if (CGRectContainsPoint(detailView.bounds, point))
            alert = [[UIAlertView alloc] initWithTitle:@"dropped in details view" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        else
            alert = [[UIAlertView alloc] initWithTitle:@"dropped outside details view" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }
}

You can create an empty view which is same as your tableView Cell. When you select a row then set your cell data into the that empty view and add that empty view on tableview.

now You can drag your empty view using gesture recoginzer, once you release is out of tableviews frame than draw your graph based on that values & remove that view from current view.

Only problem at here is you have to first select the row on which you are going to perform drag-n-drop. And from second time onwards you will be use drag-n-drop feature.

This is what I have implemented in one of my app. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM