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

我面临着拖放UILabel的问题。

如何拖动标签(移动这个),并放在任何一个UIToolBar项目(即1或2或3 …),button标题应改变为标签文本。

检查图像的这个问题

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

使用自定义button作为标签,然后使用以下代码:

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

希望它可以帮助你。