我想画两条不同颜色的1px线双色线.我有很多代码,所以我在尝试解释之后就把它放了.
如果我使用的UIView drawRect方法它,当我关掉抗锯齿工作正常但是当我使用层的drawLayerInContext,要么显示一个简单的线,或者与关闭抗锯齿2px的的或两个半透明线与antialising上2px的2个平原线行.
我通过使用自定义上下文创建一个可以指定比例的图像,设法获得了与UIView的drawRect方法类似的行为:
UIGraphicsBeginImageContextWithOptions([self bounds].size, NO, 0.f); // 0.f for scale means "scale for device's main screen".
Run Code Online (Sandbox Code Playgroud)
我只想知道为什么我没有得到相同的行为drawInContext,如果有办法得到类似的行为.
以下是绘制双色线的代码:
void DRAW_DOUBLE_LINE(CGContextRef ctx, CGPoint startPoint, CGPoint endPoint, UIColor* topColor, UIColor* bottomColor)
{
UIGraphicsPushContext(ctx);
UIBezierPath *topLine = [[UIBezierPath alloc] init];
CGPoint topLineStartPoint = startPoint;
CGPoint topLineEndPoint = endPoint;
[topLine moveToPoint:topLineStartPoint];
[topLine addLineToPoint:topLineEndPoint];
[topColor setStroke];
topLine.lineWidth = 0.5;
[topLine stroke];
UIBezierPath *bottomLine = [[UIBezierPath alloc] init];
CGPoint bottomLineStartPoint = topLineStartPoint;
bottomLineStartPoint.y +=0.5;
CGPoint bottomLineEndPoint = topLineEndPoint;
bottomLineEndPoint.y …Run Code Online (Sandbox Code Playgroud)