我在DragView
容器内有DropView
个对象和UIView
个对象,它们都是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;
}
}
答案 0 :(得分:1)
而不是这样做:
currentDragView.center = touchLocation;
尝试更改为:
currentDragView.center = [gesture translationInView:currentDropView];
更新
尝试切换这一行:
static DragView *currentDragView;
对此:
DragView *currentDragView = gesture.view;