CALayer drawrect Anti-Aliasing无效

时间:2017-01-06 11:08:25

标签: ios uinavigationcontroller calayer drawrect cgcontext

enter image description here 我通过覆盖drawInContext:(CGContextRef)ctx方法绘制波浪图层,因为波浪线是正弦波,因此我通过计算波浪值将其切割成段并将它们连接到CGMutablePathRef

以下是我在-(void)drawInContext:(CGContextRef)ctx

中所做的工作
//join style
CGContextSetLineJoin(ctx, kCGLineJoinRound);
CGContextSetLineCap(ctx, kCGLineCapRound);

CGContextSetAllowsAntialiasing(ctx, true);
CGContextSetShouldAntialias(ctx, true);

CGContextSetFillColorWithColor(ctx, [UIColor themeColor].CGColor);
CGContextSetStrokeColorWithColor(ctx, [UIColor themeColor].CGColor);

CGContextSaveGState(ctx);

CGMutablePathRef mutablePath = CGPathCreateMutable();

CGPathMoveToPoint(mutablePath, NULL, 0, 0);
CGPathAddLineToPoint(mutablePath, NULL, 0, [self getSinPoint:0 WaveHeight:self.waveHeight]);

//Calculate the Sin funciton value to draw lines and join them into path
for (float i = 0; i <= self.bounds.size.width; i+= 1) {
    NSInteger pointY = [self getSinPoint:i WaveHeight:self.waveHeight];
    CGPathAddLineToPoint(mutablePath, NULL, i, pointY);
}

CGPathAddLineToPoint(mutablePath, NULL, self.bounds.size.width, 0);

CGPathCloseSubpath(mutablePath);

[[UIColor themeColor]set];


CGContextAddPath(ctx, mutablePath);

CGContextFillPath(ctx);

CGContextRestoreGState(ctx);

1 个答案:

答案 0 :(得分:1)

enter image description here

一种解决方案是使用BezierPath绘制波形, 1.将线切割成20段 2.如果当前行索引为事件,则将控制点置于当前段的中心以下 3.如果当前行索引为奇数,则将控制点置于行中心

OnInit

获取分段线控制点的方法

...
for (NSInteger i = 0; i <= self.waveCount; i++) {

    CGPoint beginPoint = CGPointMake(i * self.waveWidth, pointY);
    CGPoint endPoint = CGPointMake((i + 1) * self.waveWidth, pointY);
    if (i%2 == 0) {

        CGPoint controlPoint = [self getCenterControlPointUp:beginPoint End:endPoint];
        CGPathAddQuadCurveToPoint(mutablePath, NULL, controlPoint.x, controlPoint.y, endPoint.x, endPoint.y);
    }else{
        CGPoint controlPoint = [self getCenterControlPointDown:beginPoint End:endPoint];
        CGPathAddQuadCurveToPoint(mutablePath, NULL, controlPoint.x, controlPoint.y, endPoint.x, endPoint.y);
    }
}
 ...