2011-12-02 458 views
2

我希望爲背景圖片中的每個圖片添加背景圖片bgImageView.image = [UIImage imageNamed:@"frame_trans"];,但滾動視圖展開而不是在每張圖片後面添加圖片,我做了什麼錯了?如何添加滾動視圖中的背景圖片

編輯:添加了工作代碼,感謝ColdLogic。

這是我的老滾動視圖

enter image description here

我想加入一個回形針

enter image description here

,但當前的代碼做到這一點。

enter image description here

CODE:

//init scrollview in location on screen 
//scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 100, 278, 290)]; 
//scrollview.backgroundColor = [UIColor redColor]; 
//pass image filenames 
NSMutableArray *fileNames = Info.imageFiles; //[[[NSMutableArray alloc] init] autorelease]; 
//setup the array of uiimageviews 
NSMutableArray *imgArray = [[NSMutableArray alloc] init]; 
NSMutableArray *bgImgArray = [[NSMutableArray alloc] init];  
//loop through the array imgNames to add file names to imgArray 
for (NSString *imageName in fileNames) { 
    NSString *tempIMGName = [NSString stringWithFormat:@"data/%i/%@",Info.ID, imageName]; 
    NSLog(@"tempIMG %@",tempIMGName); 
    UIImageView *imageView = [[UIImageView alloc] init]; 
    UIImageView *bgImageView = [[UIImageView alloc] init];; 
    imageView.image = [UIImage imageNamed:tempIMGName]; 
    bgImageView.image = [UIImage imageNamed:@"frame_trans"]; 
    imageView.contentMode = UIViewContentModeScaleAspectFit; 
    CGAffineTransform newTransform = CGAffineTransformMakeRotation((CGFloat)(-3 * M_PI/180.0)); 
    imageView.layer.affineTransform = newTransform;//CGAffineTransformMakeRotation(degreesToRadians(10)); 
    [imgArray addObject:imageView]; 
    [bgImgArray addObject:bgImageView]; 
    [imageView release]; 
    [bgImageView release]; 
} 

CGSize pageSize = scrollview.frame.size; 

NSUInteger page = 0; 

for (UIView *bgViewForScrollView in bgImgArray) { 

    [scrollview addSubview:bgViewForScrollView]; 

    bgViewForScrollView.frame = CGRectMake(pageSize.width * page++ +10, 0, pageSize.width -20 , pageSize.height); 


    // making use of the scrollView's frame size (pageSize) so we need to; 
    // +10 to left offset of image pos (1/2 the gap) 
    // -20 for UIImageView's width (to leave 10 gap at left and right) 

} 

NSUInteger page1 = 0; 
for (UIView *viewForScrollView in imgArray) { 

    [scrollview addSubview:viewForScrollView]; 
    viewForScrollView.frame = CGRectMake(pageSize.width * page1++ +10, 0, pageSize.width -20 , pageSize.height); 

    // making use of the scrollView's frame size (pageSize) so we need to; 
    // +10 to left offset of image pos (1/2 the gap) 
    // -20 for UIImageView's width (to leave 10 gap at left and right) 
} 

//add scroll view to view 
[self.view addSubview:scrollview]; 
[self.view bringSubviewToFront:scrollview]; 
scrollview.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height); 

//scrollview.contentSize = CGSizeMake(320 *viewcount + 20, 290); 
scrollview.showsHorizontalScrollIndicator =NO; 
[scrollview setPagingEnabled:YES]; 
scrollview.delegate =self; 
+0

我不明白你的問題或問題。請嘗試澄清。 – ColdLogic

+0

感謝您的回覆coldlogic,添加圖片來澄清 – Desmond

回答

2

的問題是,你正在使用page++當你創建一個後臺視圖的幀,然後不進行重置它,當你開始創建的圖像幀。頁面將有一個值爲3的imgArray進入for循環,使它位於第4個索引處。

for (UIView *bgViewForScrollView in bgImgArray) { 
    //code 
} 

//ADD THIS LINE OF CODE 
page = 0; 

for (UIView *viewForScrollView in imgArray) { 
    //Code 
} 
+0

OMG,感謝您的幫助Coldlogic ..........我多麼希望我可以上你的代表更多:) – Desmond

1

你應該添加背景圖像數以滾動視圖,並給每個人增加了回形針圖像的子視圖。

[bgImageView addSubview:imageView];