簡體   English   中英

UIView拖放-調用addsubview:時,拖動視圖消失了嗎?

[英]UIView drag and drop - drag view disappears when addsubview: is called?

我在UIView容器中有DragView對象和DropView對象,它們都是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;

    }

}

在此處輸入圖片說明

而不是這樣做:

currentDragView.center = touchLocation;

嘗試更改為此:

currentDragView.center = [gesture translationInView:currentDropView];

更新:

嘗試切換以下行:

static DragView *currentDragView;

對此:

DragView *currentDragView  = gesture.view;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM