IOS 5.1拖放 [英] IOS 5.1 drag and drop

查看:104
本文介绍了IOS 5.1拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IOS上是一个noob。我搜索了很多,但我没有找到任何关于拖放在IOS上的好教程。我刚才读到的并不是直接支持的。是否可以将项目从滚动视图拖动到视图,并传递一些信息?想象一下MAIL应用程序我想将电子邮件拖到右侧的大图上,并传递一些信息。有没有可以教我如何做的书或教程?



TKS!

解决方案

这只是使用手势识别器的问题(请参阅事件处理程序指南)。



实际的实现取决于你想要做的事情。这是一个随机的例子,我有一个分割视图控件,我将左侧的表视图拖到右侧的视图中,整个拖放由长按触发(例如点击并按住 )。因此,我只是在tableview控件的viewDidLoad(在我的例子中,主视图控制器)中创建一个手势识别器:

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

然后我定义了一个实现拖放的手势识别器处理程序,例如

   - (IBAction)longPress:(UIGestureRecognizer *)sender 
{
if(sender.state == UIGestureRecognizerStateBegan)
{
//找出表中选择的项目

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

inDrag = YES;

//获取要拖动的项目的文本

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

//创建要拖动的项目,在这个例子中,只是一个简单的UILabel

UIView * splitView = self.splitViewController.view;
CGPoint point = [sender locationInView:splitView];
UIFont * font = [UIFont systemFontOfSize:12];
CGSize size = [text sizeWithFont:font];
CGRect框架= 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]];

//现在添加项目到视图

[splitView addSubview:draggedView];
}
else if(sender.state == UIGestureRecognizerStateChanged&&& inDrag)
{
//我们拖动它,所以让我们更新拖动视图$ b的坐标
$ b UIView * splitView = self.splitViewController.view;
CGPoint point = [sender locationInView:splitView];
draggedView.center = point;
}
else if(sender.state == UIGestureRecognizerStateEnded&&& inDrag)
{
//我们删除,所以从视图中删除

[draggedView removeFromSuperview];

//,我们来弄清楚我们放弃的地方

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

UIAlertView * alert;
if(CGRectContainsPoint(detailView.bounds,point))
alert = [[UIAlertView alloc] initWithTitle:@放在细节视图消息:nil delegate:nil cancelButtonTitle:@OkotherButtonTitles:nil ]。
else
alert = [[UIAlertView alloc] initWithTitle:@删除外部细节视图消息:nil delegate:nil cancelButtonTitle:@OkotherButtonTitles: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)。


I'm a noob on IOS. I have searched a lot, but I didn't find any good tutorial about Drag and Drop on IOS. I just read that is not directly supported. Is that possible to Drag an item from a scroll view to a view, and pass some information with it? Imagine the MAIL app. I want to drag an email to the big view on the right, and pass some information with it. Is there any book, or tutorial, that can teach me how to make it?

TKS!

解决方案

It's just a matter of using Gesture Recognizers (see Event Handler Guide).

Actual implementations depends a little upon how you want to do it. Here's a random example where I've got a split view control and I'm dragging something from the tableview on the left into the view on the right, the whole drag and drop triggered by a long press (e.g. a "tap and hold"). Thus, I just create a gesture recognizer in the viewDidLoad of the tableview controller (in my case, the master view controller):

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

And then I defined a gesture recognizer handler that implements the drag and drop, e.g.,

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

Clearly, if you're dragging a subview from your scrollview, you'd replace the indexPathForRowAtPoint logic with something like:

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

That would help you identify what you want to drag, but from there, the logic is very similar (except, rather than dragging the UILabel as in my example, you'd drag your objectToDrag).

这篇关于IOS 5.1拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆