加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 如何添加多个UIImageViews来分页UIScrollView?

发布时间:2020-12-14 17:37:40 所属栏目:百科 来源:网络整理
导读:我有一个UIScrollView,页面上有多个子视图启用分页:UIButton,UIwebview和UI ImageView. webview和图像都会在每个页面上发生变化.这很好用.我使用苹果 scrolling image paging example让我入门. 但是当我添加第二个UIImageView时,我所拥有的图像位置已经获得
我有一个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)];
}

解决方法

这样做的方法是制作一个(大)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);
    }
}
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读