如何将多个UIImageView添加到分页UIScrollView中?

4
我有一个启用分页的UIScrollView,页面上有多个子视图:UIButton、UIWebView和UIImageView。每一页上的webview和image都会改变,这很好用。我使用了苹果的scrolling image paging example来帮助我入门。
但是当我添加第二个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中):(当我删除 [self 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)];
}

为什么要删除 [self layoutNavScrollImages];? - lu yuan
我不会将其删除,只是想说:当我将其删除时,图像在一个页面上正确加载。我需要一种方法使图像在每个页面上加载,并仍具有页面切换动画。 - Keaulhaas
尚未找到解决方案,但是通过添加一个带有图像背景的标签而不是ImageView的解决方法可以在这个特定项目中工作。myLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"navigationbar.png"]];然后在layoutNavScrollImages中将UIImageView更改为UILabel:
  • (void)layoutNavScrollImages { UILabel *view = nil; 等等...
- Keaulhaas
1个回答

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);
    }
}
}

我希望这尽可能清晰易懂。如果有人认为这不是正确的做法,请留下评论。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接