Skip to content

Commit

Permalink
Avoid ruler time markers overlay.
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavio Bittencourt committed Oct 28, 2016
1 parent 6a8e604 commit e5d8e94
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions Source/ICGRulerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ - (instancetype)initWithFrame:(CGRect)frame widthPerSecond:(CGFloat)width themeC
return self;
}


- (void)drawRect:(CGRect)rect
{
// Drawing code
Expand All @@ -51,39 +50,54 @@ - (void)drawRect:(CGRect)rect
CGFloat majorY = baseY - majorTickLength;

NSInteger step = 0;
BOOL shoudSkip = minorTickSpace < 4.75 ? YES : NO;
NSInteger skipCount = 5;
for (CGFloat x = leftMargin; x <= (leftMargin + width); x += minorTickSpace) {

CGContextMoveToPoint(context, x, baseY);

CGContextSetFillColorWithColor(context, self.themeColor.CGColor);
if (step % multiple == 0) {
CGContextFillRect(context, CGRectMake(x, majorY, 1.75, majorTickLength));

UIFont *font = [UIFont systemFontOfSize:11];
UIColor *textColor = self.themeColor;
NSDictionary *stringAttrs = @{NSFontAttributeName:font, NSForegroundColorAttributeName:textColor};

NSInteger minutes = step / 60;
NSInteger seconds = step % 60;

NSAttributedString* attrStr;

if (minutes > 0) {
attrStr = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld:%02ld", (long) minutes, (long) seconds] attributes:stringAttrs];
void (^drawElement)() = ^() {
CGContextSetFillColorWithColor(context, self.themeColor.CGColor);
if (step % multiple == 0) {
CGContextFillRect(context, CGRectMake(x, majorY, 1.75, majorTickLength));

UIFont *font = [UIFont systemFontOfSize:11];
UIColor *textColor = self.themeColor;
NSDictionary *stringAttrs = @{NSFontAttributeName:font, NSForegroundColorAttributeName:textColor};

NSInteger minutes = step / 60;
NSInteger seconds = step % 60;

NSAttributedString* attrStr;

if (minutes > 0) {
attrStr = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld:%02ld", (long) minutes, (long) seconds] attributes:stringAttrs];
}
else {
attrStr = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@":%02ld", (long) seconds] attributes:stringAttrs];
}

[attrStr drawAtPoint:CGPointMake(x-7, majorY - 15)];


} else {
CGContextFillRect(context, CGRectMake(x, minorY, 1.0, minorTickLength));
}
else {
attrStr = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@":%02ld", (long) seconds] attributes:stringAttrs];
}

[attrStr drawAtPoint:CGPointMake(x-7, majorY - 15)];

};


if (!shoudSkip) {
drawElement();
} else if (shoudSkip && skipCount == 5) {
skipCount = 0;
drawElement();

} else {
CGContextFillRect(context, CGRectMake(x, minorY, 1.0, minorTickLength));
skipCount++;
}

step++;
}

}

#pragma mark - Private methods
Expand Down

0 comments on commit e5d8e94

Please sign in to comment.