ios - UIView拖拽-addsubview时拖动view消失 : is called?

标签 ios objective-c uiview

我在 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;

    }

}

enter image description here

最佳答案

而不是这样做:

currentDragView.center = touchLocation;

试着改成这样:

currentDragView.center = [gesture translationInView:currentDropView];

更新:

尝试切换此行:

static DragView *currentDragView;

对此:

DragView *currentDragView  = gesture.view;

关于ios - UIView拖拽-addsubview时拖动view消失 : is called?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26501562/

相关文章:

html - 在 stringWithContentsOfURL cookies 之后不保存在 UIWebView 中。 iOS

ios - 最小化窗口动画,但对于 iOS?

swift - 展开 viewForKey(UITransitionContextFromViewKey) 时为零 (Swift)

iphone - 如何在 iOS 应用程序运行时提取有关 UI 元素的信息?

ios - 使用 JSONEncoder 对类型为 Codable 的变量进行编码

iphone - 单击注释时如何将数据传递给它的 View Controller ?

objective-c - 查看过渡动画

objective-c - iOS 6.0 中的 MessageUI 问题

objective-c - 解析 XML 存储到 NSArray

ios - IOS 中的 Sqlite3 并发问题