我有一个视图控制器,它显示在拆分视图的详细视图中,由TableView和Core Plot 图表视图组成,如下所示:
我希望能够将单个 TableViewCells 拖放到 Core Plot 视图中。
由于手势识别器只能绑定到一个视图,因此最好的方法是什么?
如何为拖放移动设置动画?
关于视图控制器包含的附加问题
如果两者都包含在同一个视图控制器容器中(如在 iOS 5 中),我如何实现从一个视图控制器到另一个视图控制器的拖放。
谢谢!
我有一个视图控制器,它显示在拆分视图的详细视图中,由TableView和Core Plot 图表视图组成,如下所示:
我希望能够将单个 TableViewCells 拖放到 Core Plot 视图中。
由于手势识别器只能绑定到一个视图,因此最好的方法是什么?
如何为拖放移动设置动画?
如果两者都包含在同一个视图控制器容器中(如在 iOS 5 中),我如何实现从一个视图控制器到另一个视图控制器的拖放。
谢谢!
这是我用来从拆分视图左侧面板上的表格视图中拖动一行到右侧面板详细信息屏幕的一些代码。这似乎与您的问题相似,尽管不可否认。无论如何,我个人从长按触发拖放:
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];
}
}
您可以创建一个与 tableView Cell 相同的空视图。当您选择一行时,然后将您的单元格数据设置到该空视图中并在 tableview 上添加该空视图。
现在您可以使用手势识别器拖动您的空视图,一旦您释放超出表格视图框架而不是根据该值绘制图形并从当前视图中删除该视图。
这里唯一的问题是您必须首先选择要执行拖放操作的行。从第二次开始,您将使用拖放功能。
这是我在我的一个应用程序中实现的。希望这可以帮助。