我正在努力将UILabel
从一个UIView
拖放到另一UIView
。请检查以下代码:
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
使用此代码,我无法正确拖动。
最佳答案
子视图不能跳出其subiewView并独自进入其他视图。
您要做的是在touchesBegan
中从其parentView中删除标签。完全在同一位置但在视图中添加anotherLabel,这是两个视图的 super 视图(要从中拖放)。
现在在touchesMoved
中。根据触摸位置移动它。
并在touchesEnd
中。找到位置并在视图上循环,并找到用户将其拖动到哪个视图,并在该位置上添加新标签作为子视图。
更新基本代码(如果存在,则修复任何语法错误)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//get position
// get label on that position
[label removeFromSuperView];
newLabel = [[UILabel alloc] init]; //make newLabel iVar
newLabel.textColor = label.textColor ; // etc copy values
[self.view addSubView:newLabel]; //positioned at label
}
touchesMoved {
//getPosition
newLabel.position = position;
}
touchesEnded {
//getPosition.
self.view.subviews; // loop and find which view has the position.
UILabel *finalLabel = [[UILabel alloc] init];
finalLabel.center = newLabel.center;
[newLabel removeFromSuperView];
[ViewToBeDropped addSubview:finalLabel];
}