我有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