2013-05-28 56 views
1

我面臨着拖放UILabel的問題。如何拖動標籤並放在UIToolbarItem中iphone

如何拖動標籤(Move This)並放在UIToolBar項目(即1或2或3 ...)中的任何一個上,該按鈕標題應該作爲標籤文本而改變。

Check image for this question

+0

你有沒有用你的標籤試過['UIPanGestureRecognizer'](https://www.cocoanetics.com/2010/11/draggable-buttons-labels/)? – Amar

+0

@Amar是的,我用UIPanGestureRecognizer – iosLearner

回答

0

的UIBarButtonItem有它自己的標題,你不能把它拖到標籤

0

使用自定義按鈕,一個標籤,然後使用此代碼:

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

希望它可以幫助你。