2012-02-26 46 views
1

下面的代碼應該顯示一系列無限連接循環中的圖像。相反,它只是顯示一個完全白色的領域。感謝Aaron Hayman讓我走得這麼遠。iOS代碼顯示白色字段而不是圖像系列

- (void)layoutScrollImages 
{ 
    //Arrangement 
    NSArray *imageViews = [NSArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil]; 
    CGRect cRect = scrollView1.bounds; 
    UIImageView *cView; 
    for (int i = 0; i < imageViews.count; i++) 
    { 
     cView = [imageViews objectAtIndex:i]; 
     cView.frame = cRect; 
     [scrollView1 addSubview:cView]; 
     cRect.origin.x += cRect.size.width; 
    } 
    scrollView1.contentSize = CGSizeMake(cRect.origin.x, scrollView1.bounds.size.height); 
    scrollView1.contentOffset = CGPointMake(scrollView1.bounds.size.width, 0); //should be the center page in a 3 page setup 

} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    scrollView1.contentOffset = CGPointMake(scrollView.bounds.size.width, 0); 
} 

跟進:

沒有我做的是使任何區別。目前,我的代碼看起來像這樣,但我正在改變我能想到的一切,無濟於事。

- (void)layoutScrollImages 
{ 
    //Horizontal arrangement 
    NSObject *image1 = @"image1.png"; 
    NSObject *image2 = @"image2.png"; 
    NSObject *image3 = @"image3.png"; 
    NSArray *imageViews = [NSArray arrayWithObjects:image1, image2, image3, nil]; 
    CGRect cRect = scrollView1.bounds; 
    UIImageView *cView; 
    for (int i = 0; i < imageViews.count; i++) 
    { 
     cView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[imageViews objectAtIndex:i]]]; 
     cView.frame = cRect; 
     [scrollView1 addSubview:cView]; 
     cRect.origin.x += cRect.size.width; 
    } 
    scrollView1.contentSize = CGSizeMake(cRect.origin.x, scrollView1.bounds.size.height); 
    scrollView1.contentOffset = CGPointMake(scrollView1.bounds.size.width, 0); //should be the center page in a 3 page setup 

    [self layoutScrollImages]; 

} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    scrollView1.contentOffset = CGPointMake(scrollView.bounds.size.width, 0); 
} 

回答

1

您永遠不會啓動UIImageView。你想要做這樣的事情:

 NSArray *imageViews = [NSArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil]; 
     CGRect cRect; 
     UIImageView *cView; 
     for (int i = 0; i < imageViews.count; i++) 
     { 
      cView = [[UIImageView alloc] initWithImage:[UIImage imageName:[imageViews objectAtIndex:i]]]; 
      cRect = cView.frame; 
      cRect.origin.x += scrollView1.frame.size.width; 
      cView.frame = cRect; 
      [scrollView1 addSubview:cView]; 
      NSLog(@"imageView bounds: %@", NSStringFromCGRect(cView.frame)); 
     } 

編輯: 試試上面並查看是否有出現。這裏有一些想法:

  1. 你確定你是不是設置ImageView的有0高度/寬度?我向循環中扔了一條NSLog語句。它將打印imageView的框架。
  2. 您是否嘗試將圖像添加到self.view而不是scrollView1?這將是確保問題與scrollView1的框架無關的好方法。或使用以下內容: NSLog(@「imageView bounds:%@」,NSStringFromCGRect(scrollView1.frame));
+0

爲imageName添加了'd',但仍沒有真正改變。它可能與我寫數組的方式有關嗎? – Sneagan 2012-02-26 02:55:03

相關問題