如何添加多个UIImageViews的分页的UIScrollView?(How to add mul

2019-09-17 13:01发布

我有与页面上的多个子视图启用分页一个UIScrollView:一个UIButton,UIWebView中和的UIImageView。 无论是网页视图,并在每一页上的图像变化。 这工作得很好。 我用苹果滚动图像分页例子让我开始。

但是,当我添加第二个的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)];
}

Answer 1:

做到这一点的方法是使一个(大)的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);
    }
}
}

我希望这是尽可能明确。 如果有人认为这是不这样做请评论的正确途径。



文章来源: How to add multiple UIImageViews to paging UIScrollView?