我有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;
}
}
为什么你'currentDragView'和'currentDropView'是'static'? – gran33 2014-10-22 06:43:49
还有谁(强引用)'currentDragView'? – gran33 2014-10-22 06:45:00
@ gran33它们是“静态”的,因此当用户拖拽“DragView”时,它们在手势选择器调用之间保持不变。在'.xib'文件中初始化的父视图拥有对'currentDragView'的强引用。 – Michael 2014-10-22 06:49:59