我是IOS的菜鸟。搜索了很多,但没有找到任何关于IOS拖放的好教程。我刚读到不直接支持。是否可以将项目从滚动视图拖动到视图并传递一些信息?想象一下 MAIL 应用程序。我想将一封电子邮件拖到右侧的大视图中,并与它一起传递一些信息。有什么书或教程可以教我如何制作吗?
TKS!
我是IOS的菜鸟。搜索了很多,但没有找到任何关于IOS拖放的好教程。我刚读到不直接支持。是否可以将项目从滚动视图拖动到视图并传递一些信息?想象一下 MAIL 应用程序。我想将一封电子邮件拖到右侧的大视图中,并与它一起传递一些信息。有什么书或教程可以教我如何制作吗?
TKS!
这只是使用手势识别器的问题(请参阅事件处理程序指南)。
实际的实现在一定程度上取决于你想怎么做。这是一个随机示例,其中我有一个拆分视图控件,我将左侧的 tableview 中的某些内容拖到右侧的视图中,整个拖放由长按触发(例如“点击并按住” )。因此,我只是在 tableview 控制器的 viewDidLoad 中创建了一个手势识别器(在我的例子中,主视图控制器):
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];
}
}
显然,如果您从滚动视图中拖动子视图,您将使用以下内容替换indexPathForRowAtPoint
逻辑:
UIView *objectToDrag = nil;
CGPoint point = [sender locationInView:myView];
for (UIView *control in myView.subviews)
if (CGRectContainsPoint(control.frame, point))
objectToDrag = control;
if (!objectToDrag)
{
inDrag = NO;
return;
}
这将帮助您确定要拖动的内容,但是从那里开始,逻辑非常相似(除了在我的示例中拖动 UILabel 之外,您将拖动 objectToDrag)。