【问题标题】:Drag & drop between view controllers (contained in the same view controller)在视图控制器之间拖放(包含在同一个视图控制器中)
【发布时间】:2012-06-29 23:55:27
【问题描述】:

我有一个视图控制器,它显示在拆分视图的详细视图中,由 TableViewCore Plot chart 视图组成,如下所示:

我希望能够将单个 TableViewCells 拖放到 Core Plot 视图中。

由于手势识别器只能绑定到一个视图,因此最好的方法是什么?

如何为拖放动作设置动画?

关于视图控制器包含的其他问题

如果两者都包含在同一个视图控制器容器中(如在 iOS 5 中),我如何实现从一个视图控制器拖放到另一个视图控制器。

谢谢!

【问题讨论】:

    标签: objective-c ios ios5 uiviewcontroller uigesturerecognizer


    【解决方案1】:

    下面是一些代码,用于将拆分视图左侧面板上的表格视图中的一行拖动到右侧面板详细信息屏幕。这似乎与您的问题相似,尽管不可否认。无论如何,我个人是通过长按触发拖放:

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

    然后我的处理程序执行以下操作:

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

    【讨论】:

      【解决方案2】:

      您可以创建一个与您的 tableView Cell 相同的空视图。当您选择一行时,将您的单元格数据设置到该空视图中并将该空视图添加到 tableview。

      现在,您可以使用手势识别器拖动空视图,一旦您释放超出 tableviews 框架,然后根据该值绘制图形并从当前视图中删除该视图。

      这里唯一的问题是您必须首先选择要执行拖放操作的行。从第二次开始,您将使用拖放功能。

      这是我在我的一个应用程序中实现的。希望这可以帮助。

      【讨论】:

      • 在tableview中选中的view可以拖到tableview外吗?
      • 我认为您正在拖动的视图应该添加到底层 UIView 而不是子视图之一(即您的根 UIView 而不是 UITableView 或 Core Plot 视图)。然后放手后,您可以检查拖动视图的中心是否在核心绘图视图的范围内。这实际上很常见,并且有很多 stackoverflow 示例。 Here is another 一些代码就是这样做的
      猜你喜欢
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 2014-04-10
      • 2012-12-31
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多