2011-09-28 65 views
0

我想在圖片上方添​​加一個頁碼,例如「1 50」以及滾動視圖中圖片下方的圖片說明。iphone:如何在scrollvew中的圖像上方和下方添加文字?

我看過這個LINK,但仍然無法弄清楚如何使它發生。

這裏是我的圖片scrollview的示例代碼。我試着去找到一個樣本,可以添加文本和滾動與圖像

NSUInteger i; 
     for (i = 1; i <= kNumImages; i++) 
     { 
       NSString *imageName = [NSString stringWithFormat:@"images%d.jpg", i]; 
       UIImage *image = [UIImage imageNamed:imageName]; 
       UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

       // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
       CGRect rect = imageView.frame; 
       rect.size.height = kScrollObjHeight; 
       rect.size.width = kScrollObjWidth; 
       imageView.frame = rect; 
       imageView.tag = i; // tag our images for later use when we place them in serial fashion 
       [scrollView1 addSubview:imageView]; 
       [imageView release]; 
     } 

UPDATE:

const CGFloat kScrollObjHeight = 320; 
const CGFloat kScrollObjWidth = 280.0; 
const NSUInteger kNumImages  = 50; 


- (void)layoutScrollImages 
{ 
UIImageView *view = nil; 
NSArray *subviews = [scrollView1 subviews]; 

// reposition all image subviews in a horizontal serial fashion 
CGFloat curXLoc = 0; 
for (view in subviews) 
{ 
     if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) 
     { 
       CGRect frame = view.frame; 
       frame.origin = CGPointMake(curXLoc, 0); 
       view.frame = frame; 

       curXLoc += (kScrollObjWidth); 
     } 
} 
- (void)viewDidLoad { 

     self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 

     // 1. setup the scrollview for multiple images and add it to the view controller 
     // 
     // note: the following can be done in Interface Builder, but we show this in code for clarity 
     [scrollView1 setBackgroundColor:[UIColor blackColor]]; 
     [scrollView1 setCanCancelContentTouches:NO]; 
     scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
     scrollView1.clipsToBounds = YES;  // default is NO, we want to restrict drawing within our scrollview 
     scrollView1.scrollEnabled = YES; 

     // pagingEnabled property default is NO, if set the scroller will stop or snap at each photo 
     // if you want free-flowing scroll, don't set this property. 
     scrollView1.pagingEnabled = YES; 

     // load all the images from our bundle and add them to the scroll view 
     NSUInteger i; 
     for (i = 1; i <= kNumImages; i++) 
     { 
       NSString *imageName = [NSString stringWithFormat:@"creative%d.jpg", i]; 
       UIImage *image = [UIImage imageNamed:imageName]; 
       UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

       // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
       CGRect rect = imageView.frame; 
       rect.size.height = kScrollObjHeight; 
       rect.size.width = kScrollObjWidth; 
       imageView.frame = rect; 
       imageView.tag = i; // tag our images for later use when we place them in serial fashion 
       [scrollView1 addSubview:imageView]; 
       [imageView release]; 
     } 

     [self layoutScrollImages]; 

我想把這一點,但無法找到正確的位置,右偏移的顯示頂部

UILabel * topLabel = [[UILabel alloc] init]; 
      topLabel.text = [NSString stringWithFormat:@"%d of %d", i, kNumImages]; 
      rect.origin.x = offset; 
      rect.size.height = 30; // however large you need the label to be 
      topLabel.frame = rect; 
      offset += 30; 

回答

1

我認爲像這樣的工作:

float offset = 0; 
    for (NSUInteger i = 1; i <= kNumImages; i++) 
    { 
      // load up the image 
      NSString *imageName = [NSString stringWithFormat:@"images%d.jpg", i]; 
      UIImage *image = [UIImage imageNamed:imageName]; 
      UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

      // image.size is important here and used to determine placement 
      CGRect rect = imageView.frame; 
      rect.size = image.size; 

      // create top label, just basic will need to configure other settings like font 
      UILabel * topLabel = [[UILabel alloc] init]; 
      topLabel.text = [NSString stringWithFormat:@"%d of %d", i, kNumImages]; 
      rect.origin.x = offset; 
      rect.size.height = 30; // however large you need the label to be 
      topLabel.frame = rect; 
      offset += 30; // adding the label height 

      // set image frame since we now know the location below top label    
      rect.size = image.size; 
      rect.origin.x += offset; 
      imageView.frame = rect; 
      imageView.tag = i; 
      offset += image.size.height; // adding image height 

      // add bottom label below image 
      UILabel * bottomLabel = [[UILabel alloc] init]; 
      bottomLabel.text = imageName; // just a sample description 
      rect.origin.x += offset; 
      rect.size.height = 30; // however large you need the label to be 
      bottomLabel.frame = rect; 
      offset += 30; // adding the label height 

      [scrollView1 addSubview:topLabel]; 
      [scrollView1 addSubview:imageView]; 
      [scrollView1 addSubview:bottomLabel]; 
      [imageView release]; 
      [topLabel release]; 
      [bottomLabel release]; 

      offset += 20; // arbitrary spacing between images 
    } 
+0

在我嘗試之前,有沒有一種方法可以進行描述比較。爲每個圖像?或更好,但它更好,如果它更好的沒有循環和幻燈片差異。圖像(差異名稱文件)與描述鏈接到圖像?即時通訊仍然試圖找到這樣的樣本,哪一個會更容易的方式... – merrill

+0

我可以看到未申報的偏移量的樣本?也可以看到topLabel.text =(樣本說明) 的樣本謝謝! – merrill

+0

@merril不知道你的第一個評論是什麼意思,這會建立一個帶有描述的圖像的垂直列表,你可以使用偏移量作爲你的Y軸而不是X軸。 也從0開始抵消,你可以設置你想要的。 topLabel是你的「x的x」文本,描述你不得不提出,因爲我什麼也沒有提到可能是什麼。 只能放'bottomLabel.text = imageName'(阿哈只是看到一個複製粘貼錯誤,會修復它。) – EricLeaf

相關問題