CALayer: Single pixel line looks like 2 pixels

∥☆過路亽.° 提交于 2019-12-03 09:57:13

问题


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.


回答1:


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




回答2:


I went ahead and made an explanatory picture, in case it's helpful to anyone

The lower line on exact pixel coordinates and will look blurry... each half of the width of the line lies in a different row of pixels...

The upper line is offset by line_width/2.0 and will look sharp... it lies completely within a row of pixels.



来源:https://stackoverflow.com/questions/15096728/calayer-single-pixel-line-looks-like-2-pixels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!