UIView拖放 - 当调用addsubview:时,拖动视图会消失吗?

时间:2014-10-22 06:34:22

标签: ios objective-c uiview

我在DragView容器内有DropView个对象和UIView个对象,它们都是UIView个子类。在手势平移上调用以下selector方法。问题是当我将DragView对象拖到DropView对象上时,DragView对象消失了。我不知道我做错了什么。

 - (void)dragViewMoved:(UIPanGestureRecognizer *)gesture{

    CGPoint touchLocation = [gesture locationInView:self];

    static DragView *currentDragView;
    static DropView *currentDropView;

   if(UIGestureRecognizerStateBegan == gesture.state){

        for(DragView *dragView in self.dragViews){

            if(CGRectContainsPoint(dragView.frame, touchLocation)){
                currentDragView = dragView;
                break;
            }
        }

    }else if(UIGestureRecognizerStateChanged == gesture.state){

        currentDragView.center = touchLocation;

        for(DropView *dropView in self.dropViews){

            if(CGRectIntersectsRect(currentDragView.frame, dropView.frame))
                currentDropView = dropView;

        }

    }else if (UIGestureRecognizerStateEnded == gesture.state){

        if(CGRectIntersectsRect(currentDragView.frame, currentDropView.frame)){

            //I think the problem is happening here
            currentDragView.center = touchLocation;
            [currentDragView removeFromSuperview];
            [currentDropView addSubview:currentDragView];

        }

        currentDragView = nil;

    }

}

enter image description here

1 个答案:

答案 0 :(得分:1)

而不是这样做:

currentDragView.center = touchLocation;

尝试更改为:

currentDragView.center = [gesture translationInView:currentDropView];

更新

尝试切换这一行:

static DragView *currentDragView;

对此:

DragView *currentDragView  = gesture.view;