Skip to content

Instantly share code, notes, and snippets.

@fjolnir
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjolnir/8783383 to your computer and use it in GitHub Desktop.
Save fjolnir/8783383 to your computer and use it in GitHub Desktop.
- (void)viewDidLoad
{
[super viewDidLoad];
NSUInteger const mmWidth = 10;
CGSize const boundsSize = { mmWidth*10, 50 };
UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0);
self.view.contentMode = UIViewContentModeTop;
[[UIColor blackColor] setFill];
UIRectFill((CGRect) {
0, 0,
boundsSize.width, 0.5
});
for(NSUInteger x = 0; x < mmWidth*10; x += mmWidth) {
BOOL isCmMarker = x%(mmWidth*10) == 0;
float const width = isCmMarker ? 1.5 : 0.5;
UIRectFill((CGRect) {
floorf(x - width/2.0f) + 0.5f,
0,
width,
isCmMarker ? 15 : 10
});
}
self.view.backgroundColor = [UIColor colorWithPatternImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
}
- (void)viewDidLoad
{
[super viewDidLoad];
_rulerLayer = [CAShapeLayer new];
_rulerLayer.lineWidth = 1;
_rulerLayer.strokeColor = [UIColor blackColor].CGColor;
_mmLayer = [CAShapeLayer new];
_mmLayer.lineWidth = 10;
_mmLayer.transform = CATransform3DMakeTranslation(0, _mmLayer.lineWidth/2.0f, 0);
_mmLayer.strokeColor = _rulerLayer.strokeColor;
_mmLayer.lineDashPattern = @[@0.5, @9.5];
_cmLayer = [CAShapeLayer new];
_cmLayer.lineWidth = 15;
_cmLayer.transform = CATransform3DMakeTranslation(0, _cmLayer.lineWidth/2.0f, 0);
_cmLayer.strokeColor = _rulerLayer.strokeColor;
_cmLayer.lineDashPattern = @[@1, @99];
[self.view.layer addSublayer:_rulerLayer];
[_rulerLayer addSublayer:_mmLayer];
[_rulerLayer addSublayer:_cmLayer];
}
- (void)viewDidLayoutSubviews
{
UIBezierPath * const rulerPath = [UIBezierPath bezierPath];
[rulerPath moveToPoint:(CGPoint) { 0, 100 }];
[rulerPath addLineToPoint:(CGPoint) { self.view.bounds.size.width, 100 }];
_rulerLayer.path = [[rulerPath copy] CGPath];
_mmLayer.path = [[rulerPath copy] CGPath];
_cmLayer.path = [[rulerPath copy] CGPath];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment