gpt4 book ai didi

ios - 分页UIScrollView如何添加多个UIImageView?

转载 作者:可可西里 更新时间:2023-11-01 05:42:14 26 4
gpt4 key购买 nike

我有一个启用了分页的 UIScrollView,页面上有多个 subview :UIButton、UIwebview 和 UIImageView。每个页面上的 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 的 subview 。然后将 UIView 作为 subview 添加到 UIScrollView。

[totalView addSubview:imageView1];
[totalView addSubview:imageView2;
[totalView addSubview:buttonView1];
[totalView addSubview:buttonView2];
[totalView addSubview:webView];

[scrollView addSubview:totalView];

请务必设置正确的内容大小,否则 ScrollView 将无法工作:

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

设置 View 并标记 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

然后在每个页面上布局 View :

- (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 - 分页UIScrollView如何添加多个UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com