如何拖动标签并放在iPhone的UIToolbarItem上

时间:2013-05-28 13:00:06

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

我遇到拖放UILabel的问题。

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

Check image for this question

2 个答案:

答案 0 :(得分: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事件来移动buttol,例如:

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

希望它对你有所帮助。

答案 1 :(得分:0)

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