如何拖动一个标签并放在UIToolbarItem在iphone [英] How to drag a label and drop on UIToolbarItem in 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.
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屋!