简体   繁体   English

iOS:ScrollView中的图像行

[英]iOS: Rows of images in ScrollView

I'm just getting started with iOS development. 我才刚刚开始iOS开发。 I would like to create a view that would contain 2 rows of image thumbnails as a preview. 我想创建一个包含2行图像缩略图的视图作为预览。 It would scroll horizontally. 它会水平滚动。 I'm wondering if the best approach would be to use 2 scrollviews or place them in a tableview with 2 rows. 我想知道最好的方法是使用2个滚动视图还是将它们放置在具有2行的表格视图中。

If your thumbnail images and rows are essentially static, your best bet might be to drop your thumbnail image views directly into your scrollView. 如果您的缩略图和行本质上是静态的,那么最好的选择是将缩略图视图直接放到scrollView中。 You can layout your thumbnail subviews into two rows and as many columns as necessary with some arbitrary calculations and avoid the need to use a UITableView (which works very well for scrolling rows, but doesn't natively support columns, and as a tool isn't imo the best for this particular job). 您可以通过任意计算将缩略图子视图分为两行,并根据需要安排任意多的列,并避免使用UITableView(这对于滚动行非常有用,但它本身不支持列,而工具是“ (对于此特定工作,最好)。

If you do elect to use a UITableView, I would put it into a single UIScrollview.... Unless you specifically seek two discretely scrolling rows, I wouldn't implement this with two UIScrollViews. 如果确实选择使用UITableView,则将其放入单个UIScrollview中。...除非您专门查找两个离散滚动的行,否则我不会使用两个UIScrollViews来实现。

Here is a code snippet that suggests an approach. 这是一个建议方法的代码段。 Consider 1)All subviews should be the same size 2)The subviews will "grid" themselves according to the bounds of the containing view. 考虑1)所有子视图的大小均应相同2)子视图将根据包含视图的边界来“网格化”自身。 I wrote this snippet for a regular UIView (not a UIScrollView), but the concept will be the same: Consider size of scrollContent. 我为常规的UIView(而不是UIScrollView)编写了此代码段,但概念是相同的:请考虑scrollContent的大小。 Consider how many subviews will fit horizontally, and how many will fit vertically (hence, if you want 2 rows, the scroll view height should be just high enough to fully encompass the height of a single subview * 2): 考虑有多少子视图可以水平放置,多少子视图可以垂直放置(因此,如果您需要2行,则滚动视图的高度应该足够高以完全包含单个子视图的高度* 2):

- (void)layoutSubviews
{

//Lays out subviews in a grid to fill view.
float myWidth = self.bounds.size.width;
float myHeight = self.bounds.size.height;

if ([[self subviews] count] < 1) 
{
    return; //no subviews, must be added before layoutSubviews is called.
}

CGRect aSubviewRect = [[[self subviews] objectAtIndex:0] frame];

int numberOfColumns = (int)(myWidth / aSubviewRect.size.width);
float actualColumnWidth = myWidth / (float)numberOfColumns;
CGFloat paddingPerColumn = (actualColumnWidth - aSubviewRect.size.width);

int maxRows = (int)(myHeight / aSubviewRect.size.height);
int qtyOfViews = [[self subviews] count];
int numberOfRows = (qtyOfViews / numberOfColumns);
if ((qtyOfViews % numberOfColumns) > 0) //we need remainder row
{
    ++numberOfRows;
}

if (numberOfRows > maxRows)
{
    numberOfRows = maxRows;
    NSLog(@"More rows required than fit.");
}

float actualRowHeight = myHeight / (float)numberOfRows;
CGFloat paddingPerRow = (actualRowHeight - aSubviewRect.size.height);

for (UIView *aSubview in [self subviews])
{
    int viewIndex = [[self subviews] indexOfObject:aSubview];
    int rowIndex = (viewIndex /  numberOfColumns);
    CGFloat yOrigin = (roundf(((float)rowIndex * aSubviewRect.size.height) + (((rowIndex + 1) * paddingPerRow) / 2)));
    int columnIndex = (viewIndex % numberOfColumns);
    CGFloat xOrigin = (roundf(((float)columnIndex * aSubviewRect.size.width) + (((columnIndex + 1) * paddingPerColumn) / 2)));

    CGRect frame = [aSubview frame];
    frame.origin.y = yOrigin;
    frame.origin.x = xOrigin;
    [aSubview setFrame:frame];

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM