[英]How to drag a label and drop on UIToolbarItem in iphone
使用自定義按鈕作為標簽,然后將此代碼用作:
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事件,將臀部移動到任意位置,例如:
- (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];
}
}
}
希望對您有幫助。
UIBarButtonItem有它自己的標題,您不能在其上拖動標簽
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.