2014-10-22 110 views
0

我有DragView对象和DropView对象,它们都是UIView子类,在UIView容器中。在手势平移上调用以下selector方法。问题是,当我将DragView对象拖到DropView对象上时,DragView对象就会消失。我不知道我做错了什么。当addsubview:被调用时,UIView拖放 - 拖动视图消失?

- (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

+0

为什么你'currentDragView'和'currentDropView'是'static'? – gran33 2014-10-22 06:43:49

+0

还有谁(强引用)'currentDragView'? – gran33 2014-10-22 06:45:00

+0

@ gran33它们是“静态”的,因此当用户拖拽“DragView”时,它们在手势选择器调用之间保持不变。在'.xib'文件中初始化的父视图拥有对'currentDragView'的强引用。 – Michael 2014-10-22 06:49:59

回答

1

而不是做这个的:

currentDragView.center = touchLocation; 

试图改变这样的:

currentDragView.center = [gesture translationInView:currentDropView]; 

更新:

尝试切换此行:

static DragView *currentDragView; 

要这样:

DragView *currentDragView = gesture.view; 
+0

试过,它没有工作。当我在'currentDropView'上调用'addSubview:'时'currentDragView'被添加到'currentDropView'的'subviews'属性,它不会出现... – Michael 2014-10-22 07:36:01

+0

它解决了你的问题吗? – gran33 2014-10-23 06:29:52

+0

不,'currentDragView'是其中'currentDragView'和'currentDropView'是兄弟的容器的'subview'。 – Michael 2014-10-23 06:38:58