【问题标题】:Drag and drop UILabel from one view to another view - iOS将 UILabel 从一个视图拖放到另一个视图 - iOS
【发布时间】:2014-04-17 07:52:59
【问题描述】:

我正在努力将UILabel 从一个UIView 拖放到另一个UIView。请检查以下代码:

  CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
  UIControl *control = sender;
  control.center = point;

使用此代码我无法正确拖动。

【问题讨论】:

    标签: ios uiview drag-and-drop uilabel uitouch


    【解决方案1】:

    一个subview不能跳出它的subiewView并自己移动到其他View中。

    您需要做的是在touchesBegan 中从其父视图中删除标签。将另一个标签添加到完全相同的位置,但在两个视图的超级视图中(要从中拖放)。

    现在在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];
    }
    

    【讨论】:

    • 那么没有视图,是否可以在视图控制器中做?
    • 好的,但是拖动的东西应该对用户可见,所以隐藏并再次放置它是行不通的。有什么想法吗?
    • 我的意思是只需在视图控制器中移动一个标签
    • 感谢您的帮助。如果您分享一些代码,将会很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2011-01-31
    • 1970-01-01
    相关资源
    最近更新 更多