Pages

September 24, 2013

Get the maximum available items that could stay in a box

I have a UIView container and I need to put some items with fixed size in t.
How many objects can I have?


/** Returns the maximum number of items that could stay within a container.

 It takes into account also a minimum spacer between items.

 @param itemDimension Size of single item.
 @param itemMinSpacer Minimum space between items.
 @param containerDim Size of container.
 @return Returns the number of maximum items that can fit into the container.
 @see maxSpaceBetweenItems:withDimension:inContainerOfDimension:

 */
+ (NSInteger)maxNumberOfItemsWithDimension:(CGFloat)itemDimension minSpacer:(CGFloat)itemMinSpacer inContainerOfDimension:(CGFloat)containerDim {
    CGFloat num = (containerDim - itemMinSpacer) / (itemMinSpacer + itemDimension);
    return floorf(num);
}


Download the ready-for-use source file (.m) of Get the maximum available items that could stay in a box

No comments:

Post a Comment