2011-09-26 105 views
1

我有一個UIScrollView,它只能水平滾動。我有一種情況,我在UIScrollView的陣列中看到填充的觀點。在UIScrollView中有三個視圖共同構成一個單一組件(有許多這樣的組件)。成分含有:在UIScrollView中左右移動

  • 的UILabel
  • 的UIButton
  • 的UIView(對於基礎文本)

我已經把兩個箭頭鍵(左和右),以UIScrollView末端。在觸摸左箭頭時,我將在UIScrollView的左邊一個位置(完整的字符串顯示)和右邊的一個位置(用完整的字符串顯示),以防右箭頭按鈕。

直到現在我能夠執行左箭頭操作。但我不知道如何執行右箭頭觸摸,以及如果只顯示部分字符串,如何將完整字符串顯示爲觸摸方向鍵。

我知道這是奇怪的,我有一個UIScrollView,仍然要添加的箭頭鍵來移動它。但不能幫助它。這是客戶的要求。以下是我如何實現左箭頭行動:

-(IBAction) onClickLeftArrow 
{ 
    NSLog(@"BUTTON VALUE : %i",iButtonValue); 
    if(iButtonValue <= [m_BCListArray count]) { 
     NSArray *arr = [scrollDemo subviews]; 
     for(int j = 0; j < [arr count]; j++) { 
      UIView *view1 = [arr objectAtIndex:j]; 
      [view1 removeFromSuperview]; 
     } 
     XX = 5.0; 
     int tagcount = [m_BCListArray count]-iButtonValue; 
     NSLog(@"%@",m_BCListArray); 
     for(int i = iButtonValue; i >= 1; i--) 
     { 
      UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 120, 26)]; 
      blabel.text = [m_BCListArray objectAtIndex:[m_BCListArray count]-i]; 
      blabel.backgroundColor = [UIColor clearColor]; 
      blabel.textColor = [UIColor whiteColor]; 
      blabel.font = [UIFont systemFontOfSize:14]; 
      [scrollDemo addSubview:blabel]; 

      //underline code 
      CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:[m_BCListArray count]-1] sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap]; 
      CGRect newFrame = blabel.frame; 
      newFrame.size.height = expectedLabelSize.height; 
      blabel.frame = CGRectMake(blabel.frame.origin.x, blabel.frame.origin.y, expectedLabelSize.width+10, expectedLabelSize.height); 
      blabel.numberOfLines = 1; 
      [blabel sizeToFit]; 
      int width=blabel.bounds.size.width; 
      int height=blabel.bounds.size.height; 

      UIButton *btnContent = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [btnContent addTarget:self action:@selector(SelectButton:)forControlEvents:UIControlEventTouchDown]; 
      btnContent.tag = tagcount+1; 
      btnContent.frame=CGRectMake(XX, 2.0, width+10, height); 
      [scrollDemo addSubview:btnContent]; 
      scrollDemo.contentSize = CGSizeMake(XX+50, 32); 
      //scrollDemo.contentSize = CGSizeMake(expectedLabelSize.width,expectedLabelSize.height); 

      UIView *viewUnderline=[[UIView alloc] init]; 
      viewUnderline.frame=CGRectMake(XX, 26, width, 1); 
      viewUnderline.backgroundColor=[UIColor whiteColor]; 
      [scrollDemo addSubview:viewUnderline]; 
      [viewUnderline release]; 

      scrollDemo.contentSize = CGSizeMake(XX+width+10, 32); 
      XX = XX + width+10; 
      iRight = iRight + 1; 
      tagcount ++; 
      // iRight --; 
     } 
    } 
    iButtonValue = iButtonValue+1; 
} 
+0

請解釋更多...你是什麼意思,你必須充分顯示字符串?你是否通過轉移一個位置的意思?你想讓下一個視圖到達中心,說明它是當前選擇的視圖嗎? – booleanBoy

+0

完整的字符串我的意思是完整的字符串應該適合屏幕(不是半隱藏的)。一個很長的字符串將是一個例外。 – Nitish

+0

笏關於字符串的位置?它應該集中還是像這樣的事情? – booleanBoy

回答

1

設置的UIScrollView的contentOffset點擊按鈕的動作,並獲得日子會把你你想要的效果。