Ios 如何在UIScrollView中相邻布置图像?
我正在下载一组图像,并将它们并排显示在可水平滚动的Ios 如何在UIScrollView中相邻布置图像?,ios,objective-c,uiview,Ios,Objective C,Uiview,我正在下载一组图像,并将它们并排显示在可水平滚动的UiScrollView中 在Android上,我在滚动视图中有一个线性布局,并设置了适当的重力。下载图像时,图像将添加到布局中并以正确的比例适当显示,拉伸父视图以容纳它们 在iOS上,我的日子不好过。我正在使用一个加载图像,这很好。问题是,在我知道它们将是什么之前,我必须初始化每个视图及其边界 for(NSString* url in self.imageURLs){ AsyncImageView *iView = [[AsyncIma
UiScrollView
中
在Android上,我在滚动视图中有一个线性布局
,并设置了适当的重力。下载图像时,图像将添加到布局中并以正确的比例适当显示,拉伸父视图以容纳它们
在iOS上,我的日子不好过。我正在使用一个加载图像,这很好。问题是,在我知道它们将是什么之前,我必须初始化每个视图及其边界
for(NSString* url in self.imageURLs){
AsyncImageView *iView = [[AsyncImageView alloc] initWithFrame:self.imageScroller.frame];
[iView showActivityIndicator];
[iView setImageURL:[NSURL URLWithString:url]];
[self.imageScroller addSubview:iView];
}
这不起作用-所有图像都被加载到另一个图像上,并按比例拉伸以适合帧
在iOS中有没有一种简单的方式来实现类似“重力”或“浮动”的布局?如果没有,我应该如何布置我的图像
编辑
需要明确的是:这些图像具有不同的纵横比,因此在加载之前,我无法知道它们的尺寸
这是我的Android布局
您应该手动设置每个iView的位置(帧)。
例如,对于垂直放置:
CGFloat y = 0;
for(NSString* url in self.imageURLs){
CGRect frame = self.imageScroller.bounds;
frame.origin.y = y;
y = y + frame.size.height;
AsyncImageView *iView = [[AsyncImageView alloc] initWithFrame:frame];
[iView showActivityIndicator];
[iView setImageURL:[NSURL URLWithString:url]];
[self.imageScroller addSubview:iView];
}
同时,您应增加imageScroller内容大小,以便能够滚动:
self.imageScroller.contentSize = CGSizeMake(y, self.imageScroller.frame.width);
试试这个:
CGRect currentFrame = self.imageScroller.bounds;
for(NSString* url in self.imageURLs){
AsyncImageView *iView = [[AsyncImageView alloc] initWithFrame:currentFrame];
[iView showActivityIndicator];
[iView setImageURL:[NSURL URLWithString:url]];
[self.imageScroller addSubview:iView];
currentFrame.origin.x += current.frame.size.width; // that is if you put them next to each other horizontally, otherwise use origin.y and size.height
}
如果还没有设置imageScroller的contentSize,请记住设置它。好的,因此最简单的方法就是计算并设置每个图像的帧
int i = 0;
float width = self.imageScroller.frame.size.width;
float height = self.imageScroller.frame.size.height;
float y = 0;
for(NSString* url in self.imageURLs){
++i;
float x = i * width;
AsyncImageView *iView = [[AsyncImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[iView showActivityIndicator];
[iView setImageURL:[NSURL URLWithString:url]];
[self.imageScroller addSubview:iView];
}
但是,如果要加载大量图像,则会出现问题
最好的方法是使用UIPageViewController。看看像这里这样的地方 最后,通过将数据添加到ajax负载,这是最简单的正确操作:
CGRect currentFrame = self.imageScroller.bounds;
CGFloat currentX = self.imageScroller.bounds.origin.x;
for(NSDictionary* snap in self.imageURLs){
NSNumber *iWidth = [snap objectForKey:@"width"];
NSNumber *iHeight = [snap objectForKey:@"height"];
CGFloat width = [iWidth floatValue] * currentFrame.size.height / [iHeight floatValue];
CGRect bounds = CGRectMake(currentX, currentFrame.origin.y, width, currentFrame.size.height);
AsyncImageView *iView = [[AsyncImageView alloc] initWithFrame:bounds];
[iView showActivityIndicator];
[iView setImageURL:[NSURL URLWithString:[snap objectForKey:@"url"]]];
[self.imageScroller addSubview:iView];
currentX += width;
}
self.imageScroller.contentSize = CGSizeMake(currentX,currentFrame.origin.y);
你可以。。。为每个图像手动设置帧,使用自动布局,使用tableview,使用collection视图,使用UIPageViewController,其他。您能解释其中一个吗?来自安卓系统的iOS文档可能是完全无法穿透的。如果你可以生活在支持iOS 6+的环境中,那么你真的应该去看看。这是一个UI
组件,可以完全满足您的要求。@rckoenes我只针对iOS 6+,我来看看。请显示所需布局的图像(显示您的android屏幕)。