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