[英]Which init method for UIView will be called when using drag and drop in xib file
[英]UIView drag and drop - drag view disappears when addsubview: is called?
我在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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.