【发布时间】:2014-03-27 16:24:46
【问题描述】:
我想知道绘制单点线的最佳方法是什么? 我的目标是在 tableViewCell 中绘制这条线,使其看起来就像原生单元格分隔符。 我不想使用原生分隔符,因为我想制作不同的颜色和不同的位置(不是底部..)。
起初我使用 1px UIView 并将其着色为灰色。但在 Retina 显示器中,它看起来像 2px。 也试过用这个方法:
- (void)drawLine:(CGPoint)startPoint endPoint:(CGPoint)endPoint inColor:(UIColor *)color {
CGMutablePathRef straightLinePath = CGPathCreateMutable();
CGPathMoveToPoint(straightLinePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(straightLinePath, NULL, endPoint.x, endPoint.y);
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = straightLinePath;
UIColor *fillColor = color;
shapeLayer.fillColor = fillColor.CGColor;
UIColor *strokeColor = color;
shapeLayer.strokeColor = strokeColor.CGColor;
shapeLayer.lineWidth = 0.5f;
shapeLayer.fillRule = kCAFillRuleNonZero;
[self.layer addSublayer:shapeLayer];
}
由于某种原因,它在 60% 的时间里都能正常工作。它有什么问题吗? 无论如何,我很乐意听到更好的方法。
谢谢。
【问题讨论】:
-
你试过改变 1px UIView 的 alpha 吗?它看起来会更薄。
-
我正在寻找一种更通用的方式,无论如何我需要一个纯色:/
-
虽然这是一个很棒的 QA - 这里有一个更现代的解决方案 stackoverflow.com/a/34766567/294884
标签: ios uitableview retina-display