iOS   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何添加多个UIImageViews来分页UIScrollView?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我有一个UIScrollView,页面上有多个子视图启用分页:UIButton,UIwebview和UI ImageView. webview和图像都会在每个页面上发生变化.这很好用.我使用苹果 scrolling image paging example让我入门. 但是当我添加第二个UIImageView时,我所拥有的图像位置已经获得了新值,并且新图像不会显示. 这是viewdidload中第一
我有一个UIScrollView,页面上有多个子视图启用分页:UIButton,UIwebview和UI ImageView. webview和图像都会在每个页面上发生变化.这很好用.我使用苹果 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)];
}
@H_674_43@解决方法
这样做的方法是制作一个(大)UIView并将每个UIImageView添加为该UIView的子视图.然后将uIView作为子视图添加到UIScrollView.

[@R_953_10586@lView addSubview:imageView1];
[@R_953_10586@lView addSubview:imageView2;
[@R_953_10586@lView addSubview:buttonView1];
[@R_953_10586@lView addSubview:buttonView2];
[@R_953_10586@lView addSubview:webView];

[scrollView addSubview:@R_953_10586@lView];

请确保设置正确的内容大小或滚动视图不起作用:

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

设置视图并标记UIView(在viewdidload内部):

NSUInteger i;
for (i = 1; i <= kNumImages; i++)
   {
      //... code to setup views
      @R_953_10586@lView.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);
    }
}
}

我希望这一点尽可能清楚.如果有人认为这不是正确的方法,请评论.

大佬总结

以上是大佬教程为你收集整理的ios – 如何添加多个UIImageViews来分页UIScrollView?全部内容,希望文章能够帮你解决ios – 如何添加多个UIImageViews来分页UIScrollView?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。