问题
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
回答1:
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.
回答2:
UIBarButtonItem has it's own title, you can't drag label on it
来源:https://stackoverflow.com/questions/16792771/how-to-drag-a-label-and-drop-on-uitoolbaritem-in-iphone