将uilabel从一个视图拖放到另一个视图-iOS

Mac*_*lay 1 drag-and-drop uiview uilabel uitouch ios

我正在努力将uilabl从一个视图拖放到另一个视图.请检查以下代码并帮助我.

  CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
  UIControl *control = sender;
  control.center = point;
Run Code Online (Sandbox Code Playgroud)

使用下面的代码无法正确拖动.

Raj*_*jat 5

子视图无法跳出其子视图并自行移动到其他视图中.

您需要做的是touchesBegan从其parentView 中删除标签.将anotherLabel精确地添加到同一位置,但在视图中,这是两个视图的超级视图(要拖放和放入).

现在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];
}
Run Code Online (Sandbox Code Playgroud)