如何拖动一个标签并放在UIToolbarItem在iphone [英] How to drag a label and drop on UIToolbarItem in iphone

查看:153
本文介绍了如何拖动一个标签并放在UIToolbarItem在iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



检查此问题的图片

解决方案

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

  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事件来移动所需的任意位置,例如: b

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

//这里使用它来检查相交,并检查您正在移动的项目的框架是否与子视图的框架相交
(UIView * anotherBtn in self.view .subviews){

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

希望它能帮助你。 / p>

I am facing problem to drag and drop UILabel .

How to drag label(Move This) and drop on any one of UIToolBar item (i.e., 1 or 2 or 3 ...) that button title should change as label text.

Check image for this question

解决方案

Use custom button as a label and then use this code as :

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

Then you may move the buttol wherever you want, by responding to the UIControlEventTouchDragInside event, e.g.:

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

Hope it helps you.

这篇关于如何拖动一个标签并放在UIToolbarItem在iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆