1

我在拖放 UILabel 时遇到问题。

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

检查此问题的图像

4

2 回答 2

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

希望它可以帮助你。

于 2013-05-28T13:22:36.767 回答
0

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

于 2013-05-28T13:16:59.240 回答