This problem should have been solved a million times, but Google was not my friend.
I need to programmatically place a set of boxes to fill a certain length and be separated by a certain distance.
This is what I want:
alt text http://img257.imageshack.us/img257/3362/spacingiwant.png
Here is what I get:
alt text http://img194.imageshack.us/img194/3506/spacingiget.png
Since I work in Objective-C using Core Graphics, I need a series of Rects that I can draw or swing an image into. My naive attempt draws a set of boxes at a certain interval, but leaves space at the end.
Here is my code that is in the drawRect method: method
CGContextRef context = UIGraphicsGetCurrentContext(); CGFloat barStartX = 96; CGFloat barStartY = 64.0; CGFloat barWidth = 16; CGFloat barHeight = 64; CGFloat barGutter = 8; int barSegments = 8; for (int segmentNumber = 0; segmentNumber <= (barSegments - 1); ++segmentNumber) {
Before creating an impenetrable monster of a one-time code that I donβt even understand in 6 months, I wonder if there is a general solution to this problem with an interval.
The answer does not have to be in Objective-C if it is somewhat similar to C. Readability takes precedence over performance considerations.
source share