5

我正在画很多充满 NSGradient 的梯形。我希望他们能够无缝加入,但事实并非如此。在这种情况下,我更喜欢使用剪辑而不是使用渐变显式填充 NSBezierPath。

两个裁剪区域之间的间隙

以下是涉案代码:

-(void)drawRect:(NSRect)dirtyRect {
    [[NSColor blackColor]set];
    NSRectFill(self.bounds);

    NSPoint v0 =  NSMakePoint(100,100);
    NSPoint v1 =  NSMakePoint(200,100);
    NSPoint v2 =  NSMakePoint(100,200);
    NSPoint v3 =  NSMakePoint(200,200);
    NSPoint v4 = NSMakePoint(150, 150);

    NSBezierPath * triangle0 = [NSBezierPath bezierPath];
    [triangle0 moveToPoint:v0];
    [triangle0 lineToPoint:v1];
    [triangle0 lineToPoint:v2];
    [triangle0 closePath];

    NSBezierPath * triangle1 = [NSBezierPath bezierPath];
    [triangle1 moveToPoint:v1];
    [triangle1 lineToPoint:v2];
    [triangle1 lineToPoint:v3];
    [triangle1 closePath];

    NSGradient * gradient = [[NSGradient alloc] initWithColorsAndLocations:[NSColor whiteColor],0.0,[NSColor orangeColor],1.0,nil];

    [NSGraphicsContext saveGraphicsState];
    [triangle0 addClip];
    [gradient drawFromPoint:v0 toPoint:v4 options:NSGradientDrawsAfterEndingLocation|NSGradientDrawsBeforeStartingLocation];
     //   [gradient drawFromPoint:v0 toPoint:v4 options:0];

    [NSGraphicsContext restoreGraphicsState];

    [NSGraphicsContext saveGraphicsState];
    [triangle1 addClip];
    [gradient drawFromPoint:v3 toPoint:v4 options:NSGradientDrawsAfterEndingLocation|NSGradientDrawsBeforeStartingLocation];

    //[gradient drawFromPoint:v3 toPoint:v4 options:0];

    [NSGraphicsContext restoreGraphicsState];

我应该怎么做才能避免两个三角形之间的间隙?

编辑:这是通过显式填充 NSBezierPath 来解决此问题的尝试。

-(void)drawRect:(NSRect)dirtyRect {
[[NSColor blackColor]set];
NSRectFill([self bounds]);

CGContextRef c = [[NSGraphicsContext currentContext] graphicsPort];
CGContextTranslateCTM(c, 0.5, 0.5);

NSPoint v0 =  NSMakePoint(100,100);
NSPoint v1 =  NSMakePoint(200,100);
NSPoint v2 =  NSMakePoint(100,200);
NSPoint v3 =  NSMakePoint(200,200);

NSBezierPath * triangle0 = [NSBezierPath bezierPath];
[triangle0 moveToPoint:v0];
[triangle0 lineToPoint:v1];
[triangle0 lineToPoint:v2];
[triangle0 closePath];

NSBezierPath * triangle1 = [NSBezierPath bezierPath];
[triangle1 moveToPoint:v1];
[triangle1 lineToPoint:v2];
[triangle1 lineToPoint:v3];
[triangle1 closePath];

[[NSColor whiteColor]set];

NSGradient * gradient = [[NSGradient alloc] initWithColorsAndLocations:[NSColor whiteColor],0.0,[NSColor orangeColor],1.0,nil];
[gradient drawInBezierPath:triangle0 angle:45];
[gradient drawInBezierPath:triangle1 angle:45+180];

NSBezierPath  * seam = [ NSBezierPath bezierPath]; [seam moveToPoint:v1]; [seam lineToPoint:v2];
[[NSColor orangeColor]set]; [seam stroke];

NSGradient * gradient2 = [[NSGradient alloc] initWithColorsAndLocations:[NSColor whiteColor],0.0,[NSColor orangeColor],0.5,[NSColor whiteColor],1.0,nil];
NSBezierPath * squarePath = [NSBezierPath bezierPath];
[squarePath moveToPoint:v0];
[squarePath lineToPoint:v1];
[squarePath lineToPoint:v3];
[squarePath lineToPoint:v2];
[squarePath closePath];
CGContextTranslateCTM(c, 200, 0);
[gradient2 drawInBezierPath:squarePath angle:45];
}

两条路径之间的接缝

在左侧,两个三角形填充了相同的 NSGradient。在右边,一个充满了 NSGradient 的正方形,这是我想要达到的目标。接缝是试图隐藏两个 NSBezierPath 之间的黑线,但它并没有真正起作用。在这种情况下,它几乎不可见,但如果我绘制大量这样的三角形,它将是可见的。

编辑 Rengers 答案:

这是没有抗锯齿的外观:

没有抗锯齿,但问题已经转移

从技术上讲,这是一个很好的答案,但现在的问题是,不出所料,多边形的边缘不再抗锯齿。知道如何在不丢失边缘抗锯齿的情况下消除剪切区域之间的间隙吗?

4

1 回答 1

2

这可能是因为默认情况下启用了抗锯齿。您可以使用以下方法禁用它:

[[NSGraphicsContext currentContext] setShouldAntialias:NO];
于 2012-11-01T16:18:34.737 回答