2013-05-28 50 views
1

您好我是ios開發新手。我試圖將標籤從視圖拖動到工具欄...但標籤將位於工具欄的背面。我希望標籤拖動工具欄的前面..請幫助我解決這個問題..... 。這是我到現在爲止的代碼如何從一個視圖拖動對象到另一個視圖

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIToolbar *backgrdtoolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,44)]; 

    [backgrdtoolbar setBarStyle:UIBarStyleBlackOpaque]; 

    [self.view addSubview:backgrdtoolbar]; 

    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)]; 

    [mainScroll setContentSize:CGSizeMake(600,45)]; 
    [mainScroll setBackgroundColor:[UIColor clearColor]]; 
    [mainScroll setShowsHorizontalScrollIndicator:NO]; 

    mainToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 600, 44)]; 
    [mainToolbar setBarStyle:UIBarStyleBlackTranslucent]; 

    [self.view addSubview:mainScroll]; 

    [mainScroll addSubview:mainToolbar]; 

    UIBarButtonItem *b1 = [[UIBarButtonItem alloc]initWithTitle:@"1" style:UIBarButtonItemStyleBordered target:self action:@selector(_1:)]; 
    UIBarButtonItem *b2 = [[UIBarButtonItem alloc]initWithTitle:@"2" style:UIBarButtonItemStyleBordered target:self action:@selector(_1:)]; 
    UIBarButtonItem *b3 = [[UIBarButtonItem alloc]initWithTitle:@"3" style:UIBarButtonItemStyleBordered target:self action:@selector(_1:)]; 

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:b1,spacer,b2,spacer,b3, spacer,b4,spacer,b5,spacer,b6,spacer,b7,spacer,b8,spacer,b9,spacer,b10,nil]; 
    [mainToolbar setItems:arr]; 

} 



- (IBAction)handle:(UIPanGestureRecognizer *)gesture { 
     static CGRect originalFrame; 

     if (gesture.state == UIGestureRecognizerStateBegan){ 
      originalFrame = gesture.view.frame; 
     } else if (gesture.state == UIGestureRecognizerStateChanged) { 
      CGPoint translate = [gesture translationInView:gesture.view.superview]; 

      CGRect newFrame = CGRectMake(fmin(gesture.view.superview.frame.size.width - originalFrame.size.width, fmax(originalFrame.origin.x + translate.x, 0.0)), 
             fmin(gesture.view.superview.frame.size.height - originalFrame.size.height, fmax(originalFrame.origin.y + translate.y , 0.0)), 
             originalFrame.size.width, 
             originalFrame.size.height); 


      gesture.view.frame = newFrame; 

     } 

    } 
+0

標籤在哪裏?附加的手勢是什麼? – Wain

回答

0

是否可以將一個視圖拖到另一個視圖?對的,這是可能的。

但是,如果唯一重要的是將標籤放在另一個視圖的前面,我相信最好的方法不會改變您的標籤所在的視圖。通過這個我意思是你不需要改變他人的觀點,這是你的目標視圖。

如果我在你的鞋子裏,我將使用一個視圖,它認爲你的標籤被放置爲包含兩個視圖,放置標籤的視圖和你希望標籤可以被拖動的視圖。

當你把你的標籤這一觀點,觀察重要的一點是,它必須在你的意見,你能做到這一點使用此代碼:

[view bringSubviewToFront:yourLabel]; 

之後,你可以拖動它在你的視圖容器內,它將在其他視圖前面。 :)

-

如果你真的想從一個視圖切換到另一個視圖,您必須從原來的視圖中刪除它,並把它添加到另一種觀點認爲它將被放置。類似的東西

if(gesture.view.frame.origin.y > y) { 
    [gesture.view removeFromSuperview]; 
    [newView addSubview:gesture.view]; 
    //Your code to adjust the frame for the new view, because your frame is relative 
    //It is define by your superview 
} 
+1

我會投票支持'[view bringSubviewToFront:yourLabel]'作爲比實際將標籤移動到另一個視圖更好的解決方案。 –

相關問題