gpt4 book ai didi

objective-c - 在 View Controller 之间拖放(包含在同一个 View Controller 中)

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:49:37 24 4
gpt4 key购买 nike

我有一个 View Controller ,它显示在 Split View的详细 View 中,它由一个 TableView 和一个 Core Plot 图表 View 组成,如下所示:

My view controller

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

执行此操作的最佳方法是什么,因为手势识别器只能绑定(bind)到一个 View ?

如何为拖放 Action 设置动画?

关于 View Controller 包含的附加问题

如果两者都包含在同一个 View Controller 容器中(如在 iOS 5 中),我如何实现从一个 View Controller 到另一个 View Controller 的拖放操作。

谢谢!

最佳答案

这是我用来将行从 Split View左侧面板的表格 View 拖到右侧面板详细信息屏幕的一些代码。这似乎与您的问题相似,但不可否认不完全相同。无论如何,我个人通过长按触发拖放:

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

关于objective-c - 在 View Controller 之间拖放(包含在同一个 View Controller 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11245043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com