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

ios – 如何添加多个UIImageViews来分页UIScrollView?,第1张

概述我有一个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 vIEwfor (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 fashionCGfloat 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 fashionCGfloat 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 fashionCGfloat 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?所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1010973.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存