簡體   English   中英

如何在iPhone中將標簽拖放到UIToolbarItem上

[英]How to drag a label and drop on UIToolbarItem in iphone

我面臨拖放UILabel的問題。

如何拖動標簽(“移動”)並放到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事件,將臀部移動到任意位置,例如:

- (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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM