Search code examples
objective-ccocoacore-animationcore-graphics

CALayer: Single pixel line looks like 2 pixels


This is my code:

int columns 3;
int columnWidth = self.layer.bounds.size.width / 3;

for (int c = 1; c < columns; c++) {
    CALayer *layer  = [CALayer layer];
    layer.frame = (CGRectMake(columnWidth * c + 0.5, 0.5, 1, self.layer.bounds.size.height));
    layer.backgroundColor = myColor;
    [grid addSublayer:layer];
}

I understand that I have to shift the x and y 0.5 pixels, which is what I have done, yet it still shows as a 2 pixel line instead of 1.

enter image description here


Solution

  • Set the layer Frame as

    layer.frame = (CGRectMake(columnWidth * c + 0.5, 0.5, 0.5, self.layer.bounds.size.height));
    

    Yeah it will work for retina also

    Check this post Section 1 Point Lines and Pixel Boundaries.It is well explained there