关于如何将项目从UITableView拖放到UITableView的教程 [英] Tutorial on How to drag and drop item from UITableView to UITableView

查看:109
本文介绍了关于如何将项目从UITableView拖放到UITableView的教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这个头上碰了一会儿,我想出来了。我想回到社区,因为我从这个网站获得了很多帮助:)。



我正在尝试将一个项目从一个UITableView复制到另一个UITableView和我在网上看到的关于如何做的信息一直是粗略的。我自己想出来,所以我会描述我的小建筑。




  • 大师UIView


    • UIView with UITableView


      • 自定义UITableViewCell


        • UID视图(UITableView)的UIView被复制(在我的例子中为Person对象)



    • <


      • 自定义UITableViewCell


        • 自定义UIView被复制(我的情况下的Person对象)






我在UITableView中拥有的人物对象是我要从一个表中拖放到另一个表中的对象。我最难以弄清楚如何将物品从桌子中弹出,并以一个平滑的动作将其拖动。最长的时间,为了执行这个操作,我需要两次触摸。



从Person对象开始,这是一个包含图像的简单对象。

   - (void) )touchesMoved:(NSSet *)触摸与事件:(UIEvent *)事件{
if(m_moveable == YES){
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];

if(0< location.x-50&& location.x + 50< 768){
if(0< location.y-50&& location.y + 150< 1004){
self.center = location;
}
}
}
}

在初始化时将Person对象的userInteractionEnabled标志设置为NO,以便表中的任何点击都不会被Person对象捕获。在这种情况下,Person对象将在表中移动,从而破坏目的。



下一个对象是我的自定义UITableViewCell。这个对象负责捕捉用户的第一触摸。它应该做的是抓住这个触摸,并弹出人出来。该人是属于自定义UITableViewCell的子视图之一。

   - (void)touchesMoved:(NSSet *)触摸与事件:(UIEvent *)事件{
UIView * parent = self.superview.superview.superview;

Person * s = nil;
(UIView * child in self.subviews){
if([child isKindOfClass:[Person class]]){
s = child;
s removeFromSuperview];
break;
}
}

if(s!= nil){
self.userInteractionEnabled = NO;
s.userInteractionEnabled = YES;
UITableView * subParent = self.superview; //编辑#1
subParent.scrollEnabled = NO; //编辑#1

[parent addSubview:s];
// [self touchesEnded:touches withEvent:event]; //编辑#1
}
}

请注意userInteractionEnabled标志在上述方法中翻转。触摸之前,Person对象对于一个人的触摸是不受限制的。自定义单元格捕获移动后,通过将Person添加到父视图中然后激活(userInteractionEnabled = YES)来释放该Person。 Person对象然后是诞生的,可以处理自己的移动触摸。



有一个小小的毛刺,Person对象在左上角闪烁,然后立即下降到用户的手指。



这个设计的最后一部分是主UIView需要处理触摸转换。当用户触摸桌面并且Person对象被弹出时,应用程序需要意识到需要从表中删除焦点并指向Person对象。这样做的方式是在主UIView中的hitTest方法被重载了以下。

   - (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView * rv = nil;
(UIView * child in self.subviews){
if([child isKindOfClass:[Person class]]){
rv = child;
child.center = point;
break;
}
}
if(rv == nil){
rv = [super hitTest:point withEvent:event];
}
return rv;
}

该代码的工作原理是当Person被弹出时在桌子上,它没有一个触控的焦点。触摸由UITableView拥有,人从其中弹出。 hitTest方法是重新调整该触摸的关键。通常,系统会检查哪个UIView是触摸的焦点。 hitTest方法被系统调用来识别该UIView。当Person附加到主视图时,该hitTest函数遍历所有子视图,并检测Person的存在并将其作为主导触摸的对象返回。您的手指的任何动作将立即被报告给该人而不是UITableView。



这是实现的胆量。要有一个UITableViewcatch,移动对象现在很简单,我会离开它来试试看!
如果您有任何问题,请发表他们!



编辑#1
删除Person对象证明比我想象的更难。当UITableView正在吸引所有的移动事件时,我不得不添加一行来防止UITableView滚动,因为UITableView正在吸引所有的移动事件。

在定制的UITableViewCell类中触发touchesEnded函数。

mj

解决方案

我已经设法在UITableView中创建一行拖放到另一行同一张桌子。我创建了一个表示移动项目(未从表中删除)的uiImageView,并将其附加到最前面的UIView,使其在整个屏幕上拖动。
这里有一些关于我所面临的痛苦的帖子:


  1. UITableView:将一行拖到另一个上

  2. UITableView:以编程方式滚动内容视图

  3. UITableView:自定义手势使其不再滚动

希望这可以帮助^^


I've been banging my head on this one for a while and I figured it out. I want to give back to the community since I've gotten a lot of help from this website :).

I'm trying to copy an item from one UITableView to another UITableView and information I've seen on the web regarding how to do this has been sketchy at best. I figured it out on my own and so I'll describe my little architecture.

  • Master UIView
    • UIView with UITableView
      • Custom UITableViewCell
        • Custom UIView that gets copied (Person object in my case)
    • UIView with UITableView
      • Custom UITableViewCell
        • Custom UIView that gets copied (Person object in my case)

The person object that I have in the UITableView is the object that I want to drag and drop out of one table and into another. I had the most difficult figuring out how to pop the item out of the table and drag it over in a single smooth motion. For the longest time, it would take me two touches in order to perform the operation.

Starting with the Person object, this is a simple object that contains an image. I had to implement my own touchesMoved method to change the center position of the Person when a drag is taking place.

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    if( m_moveable == YES ){
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView:self.superview];

        if( 0 < location.x-50 && location.x+50 < 768 ){ 
            if( 0 < location.y-50 && location.y+150 < 1004 ){
                self.center = location;
            }
        }
    }
}

I set the Person object's userInteractionEnabled flag to NO on initialization so that any clicks in the table would not get caught by the Person object. The Person object in that case would move within the table which defeats the purpose.

The next object is my custom UITableViewCell. This object is responsible to catch the user's first touch. What it's supposed to do is catch this touch and "pop" the Person out. The Person is one of the subviews belonging to the custom UITableViewCell.

 - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView *parent = self.superview.superview.superview;    

    Person *s = nil;
    for( UIView *child in self.subviews ){
        if( [child isKindOfClass:[Person class]] ){
            s = child;
            s removeFromSuperview];
            break;
        }        
    }

    if( s != nil ){
        self.userInteractionEnabled = NO;
        s.userInteractionEnabled = YES;
        UITableView *subParent = self.superview;   //EDIT #1
        subParent.scrollEnabled = NO;              //EDIT #1

        [parent addSubview:s];
        //[self touchesEnded:touches withEvent:event]; //EDIT #1
    }
}

It's important to note the userInteractionEnabled flag getting flipped in the above method. Before the touch, the Person object is "off limits" to a person's touch. After the custom cell catches a move, the Person is released by adding it to the parent's view and then activated (userInteractionEnabled=YES). The Person object is then "born" and can handle move touches on it's own.

This has one minor glitch in that the Person object blinks in the upper left corner but then drops down immediately to the user's finger.

The final part of this design is that the master UIView needs to handle a "touch transition." When the user is touching the table and the Person object gets popped out, the app needs to realize that focus needs to be removed from the table and directed towards the Person object. The way that this was done is that the hitTest method in the master UIView was overloaded with the following.

- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *rv = nil;
    for(UIView *child in self.subviews){
        if( [child isKindOfClass:[Person class]] ){
            rv = child;
            child.center = point;
            break;
        }
    }
    if( rv == nil ){
        rv = [super hitTest:point withEvent:event];
    }   
    return rv;
}

The way that this code works, is that when the Person is popped out of the table, it doesn't have a touch focused at it. The touch is "owned" by the UITableView from which the Person was popped out of. The hitTest method is the key to refocusing that touch. Regularly, the system checks to see which UIView is the focus of a touch. The hitTest method is called by the system to identify that UIView. When the Person is attached to the master view, this hitTest function iterates through all subviews and detects the presence of the Person and returns it as the "dominant" touched object. Any movement of your finger will immediately be reported to the Person and not to the UITableView.

This is guts of the implementation. To have a UITableView "catch" the moving object is simple now and I'll leave it for you to try! If you have any questions, please post them!

EDIT #1 Dropping the Person object is proving harder than I thought :). I had to add a line to prevent the UITableView from scrolling when the parent is being moved around because the UITableView is sucking in all the movement events.
The touchesEnded function fires in the custom UITableViewCell class.
mj

解决方案

Hi I've managed to create drag&drop of a row in a UITableView onto another row of the same table. I created a uiImageView representing the moving item (which was not removed from the table) and attached it to the frontmost UIView to made it draggable on the whole screen. Here are some post about the pains I faced:

  1. UITableView: Drag a row onto another one
  2. UITableView: scrolling programmatically the content view
  3. UITableView: custom gestures make it scrolling no more

Hope this can help ^^

这篇关于关于如何将项目从UITableView拖放到UITableView的教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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