【问题标题】:How to drag a label and drop on UIToolbarItem in iphone如何在 iphone 的 UIToolbarItem 上拖放标签
【发布时间】:2023-03-17 19:41:01
【问题描述】:

我在拖放 UILabel 时遇到问题。

如何拖动标签(Move This)并拖放到任何一个 UIToolBar 项目(即 1 或 2 或 3 ...)上,按钮标题应更改为标签文本。

Check image for this question

【问题讨论】:

标签: iphone ios drag-and-drop uilabel uitoolbar


【解决方案1】:

使用自定义按钮作为标签,然后将此代码用作:

     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(btnTouch:withEvent:) forControlEvents:UIControlEventTouchDown];
    button.tag = -1;
button.titleLabel.text = @"Move this";
        [button addTarget:self action:@selector(btnTouch:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        [self.view addSubview:button];

然后您可以通过响应 UIControlEventTouchDragInside 事件将按钮移动到任何您想要的位置,例如:

- (IBAction) btnTouch:(id) sender withEvent:(UIEvent *) event
{
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    UIControl *control = sender;
    control.center = point;

    //Here use this to check when intersects and check if the frame of the item you are moving intersects with the frame from on of your subviews
    for (UIView *anotherBtn in self.view.subviews) {

        if (CGRectIntersectsRect(control.frame, anotherBtn.frame)) {
            // Do something
            [anotherBtn setTitle:control.titleLabel.text];
        }
    }
}

希望对你有帮助。

【讨论】:

    【解决方案2】:

    UIBarButtonItem 有自己的标题,不能在上面拖标签

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      相关资源
      最近更新 更多