2012-05-27 49 views
4

我有一個UIScrollView與頁面上的多個子視圖啓用分頁:UIButton,UIwebview和UIImageView。 webview和圖像在每個頁面上都會更改。這工作正常。我用蘋果scrolling image paging example讓我開始。如何將多個UIImageView添加到分頁UIScrollView?

但是,當我添加第二個UIImageView,圖像的位置我已經到位已經獲得新的值和新的圖像不顯示。

這裏面viewDidLoad中的代碼,第一個圖像(正常工作):

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


    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
    CGRect rect = imageView2.frame; 
    rect.size.height = imageviewScrollObjHeight; 
    rect.size.width = imageviewScrollObjWidth; 

    // Get the Layer of any view 
    CALayer * imageLayer = [imageView2 layer]; 
    [imageLayer setMasksToBounds:YES]; 
    [imageLayer setCornerRadius:7.0]; 

    // You can even add a border 
    [imageLayer setBorderWidth:1.0]; 
    [imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]]; 

    imageView2.frame = rect; 
    imageView2.tag = i; // tag our images for later use when we place them in serial fashion 


    [scrollView1 addSubview:imageView2]; 

} 

[self layoutScrollImages]; // now place the photos in serial layout within the scrollview 

這是佈局的每一頁,每頁不同的圖像(外viewDidLoad中)第一個圖像的代碼(正常工作):

// layout images for imageview1 
- (void)layoutScrollImages 
{ 
UIImageView *imageView = nil; 
NSArray *subviews = [scrollView1 subviews]; 

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

     curXLoc += (kScrollObjWidth); 
    } 
} 

// set the content size so it can be scrollable 
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)]; 
} 

這是對於第二圖像的代碼(內部viewDidLoad中):(當i除去[自layoutNavScrollImages];僅在第一頁上的圖像負載)

for (i = 1; i <= kNumImages; i++) 
{ 

    UIImage *navBarImage = [UIImage imageNamed:@"navigationbar.png"]; 
    UIImageView *imageViewNavBar = [[UIImageView alloc] initWithImage:navBarImage]; 

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
    CGRect navBarRect = imageViewNavBar.frame; 
    navBarRect.size.height = 44; 
    navBarRect.size.width = 320; 
    navBarRect.origin.x = 0; 
    navBarRect.origin.y = 0; 

    /* Get the Layer of any view 
    CALayer * imageLayer = [imageView3 layer]; 
    [imageLayer setMasksToBounds:YES]; 
    [imageLayer setCornerRadius:7.0]; 

    // You can even add a border 
    [imageLayer setBorderWidth:1.0]; 
    [imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]]; 
    */ 
    imageViewNavBar.frame = navBarRect; 
    imageViewNavBar.tag = i; // tag our images for later use when we place them in serial fashion 

    [scrollView1 addSubview:imageViewNavBar]; 

} 

[self layoutNavScrollImages]; 

而且外面的viewDidLoad代碼:(這覆蓋了第一個圖像的位置)

- (void)layoutNavScrollImages 
{ 
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); 
    } 
} 

// set the content size so it can be scrollable 
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)]; 
} 
+0

你爲什麼要移除[self layoutNavScrollImages];? –

+0

我不刪除它,只是說:當我刪除它,圖像正確加載在一頁上。我需要一種方法來在每個頁面上加載圖像,並仍然具有頁面切換動畫。 – Keaulhaas

+0

尚未找到解決方案,但是通過添加標籤而不是圖像作爲背景,可用於此特定項目的解決方法。 myLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@「navigationbar.png」]]; 然後在layoutNavScrollImages中將UIImageView更改爲UILabel: - (void)layoutNavScrollImages \t UILabel * view = nil; 等... – Keaulhaas

回答

4

要做到這一點是讓一個(大)的UIView和添加的每個的UIImageView作爲的UIView的子視圖的方式。然後將UIView作爲子視圖添加到UIScrollView。

[totalView addSubview:imageView1]; 
[totalView addSubview:imageView2; 
[totalView addSubview:buttonView1]; 
[totalView addSubview:buttonView2]; 
[totalView addSubview:webView]; 

[scrollView addSubview:totalView]; 

一定要設置正確的內容大小或滾動視圖不會工作:

[scrollView setContentSize:CGSizeMake((320*kNumImages), 411)]; 

設置的意見和標籤UIView的(裏面viewDidLoad中):

NSUInteger i; 
for (i = 1; i <= kNumImages; i++) 
    { 
     //... code to setup views 
     totalView.tag = i; 
    } 
[self layoutViews]; // now place the views in serial layout within the scrollview 

然後佈局視圖在每頁上:

- (void)layoutViews 
{ 
UIView *view = nil; 
NSArray *subviews = [scrollView subviews]; 

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

     curXLoc += (kScrollObjWidth); 
    } 
} 
} 

我希望這個儘可能清楚。如果有人認爲這不是正確的做法,請發表評論。