重新粉刷 NSBezierPath



这就是我用贝塞尔路径绘制类似弹出窗口的方式。布尔值 bAbajo 使绘图窗口指向上方。如果不是bAbajo,它指向上方:

- (void)drawRect:(NSRect)dirtyRect
{
    NSRect contentRect = NSInsetRect([self bounds], LINE_THICKNESS, LINE_THICKNESS);
    NSBezierPath *path = [NSBezierPath bezierPath];
    if (_bAbajo)
    {
      [path removeAllPoints];
      [path moveToPoint:NSMakePoint(_arrowX, NSMinY(contentRect))];
      [path lineToPoint:NSMakePoint(_arrowX + ARROW_WIDTH / 2, NSMinY(contentRect) + ARROW_HEIGHT)];
      [path lineToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMinY(contentRect) + ARROW_HEIGHT)];
      NSPoint bottomRightCorner = NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect) + ARROW_HEIGHT);
      [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect) + ARROW_HEIGHT + CORNER_RADIUS)
         controlPoint1:bottomRightCorner controlPoint2:bottomRightCorner];
      [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect) - CORNER_RADIUS)];
      NSPoint topRightCorner = NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect));
      [path curveToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMaxY(contentRect) + CORNER_RADIUS) controlPoint1:topRightCorner controlPoint2:topRightCorner];
      [path lineToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMaxY(contentRect) + CORNER_RADIUS)];
      NSPoint topLeftCorner = NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect));
      [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect) - CORNER_RADIUS) controlPoint1:topLeftCorner controlPoint2:topLeftCorner];
      [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect) + CORNER_RADIUS + ARROW_HEIGHT)];
      NSPoint bottomLeftCorner = NSMakePoint(NSMinX(contentRect), NSMinY(contentRect) + ARROW_HEIGHT);
      [path curveToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMinY(contentRect) + ARROW_HEIGHT) controlPoint1:bottomLeftCorner controlPoint2:bottomLeftCorner];
      [path lineToPoint:NSMakePoint(_arrowX - ARROW_WIDTH/2, NSMinY(contentRect) + ARROW_HEIGHT)];
    }
    else
    {
      [path removeAllPoints];
      [path moveToPoint:NSMakePoint(_arrowX, NSMaxY(contentRect))];
      [path lineToPoint:NSMakePoint(_arrowX + ARROW_WIDTH / 2, NSMaxY(contentRect) - ARROW_HEIGHT)];
      [path lineToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMaxY(contentRect) - ARROW_HEIGHT)];
      NSPoint topRightCorner = NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT);
      [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT - CORNER_RADIUS)
           controlPoint1:topRightCorner controlPoint2:topRightCorner];
      [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect) + CORNER_RADIUS)];
      NSPoint bottomRightCorner = NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect));
      [path curveToPoint:NSMakePoint(NSMaxX(contentRect) - CORNER_RADIUS, NSMinY(contentRect))
           controlPoint1:bottomRightCorner controlPoint2:bottomRightCorner];
      [path lineToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMinY(contentRect))];
      [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect) + CORNER_RADIUS)
           controlPoint1:contentRect.origin controlPoint2:contentRect.origin];
      [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT - CORNER_RADIUS)];
      NSPoint topLeftCorner = NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect) - ARROW_HEIGHT);
      [path curveToPoint:NSMakePoint(NSMinX(contentRect) + CORNER_RADIUS, NSMaxY(contentRect) - ARROW_HEIGHT)
           controlPoint1:topLeftCorner controlPoint2:topLeftCorner];
      [path lineToPoint:NSMakePoint(_arrowX - ARROW_WIDTH / 2, NSMaxY(contentRect) - ARROW_HEIGHT)];
    }
    [path closePath];
    [[NSColor colorWithDeviceWhite:1 alpha:FILL_OPACITY] setFill];
    [path fill];
    //[NSGraphicsContext saveGraphicsState];
    NSBezierPath *clip = [NSBezierPath bezierPathWithRect:[self bounds]];
    [clip appendBezierPath:path];
    [clip addClip];
    [path setLineWidth:LINE_THICKNESS * 2];
    [[NSColor whiteColor] setStroke];
    [path stroke];
    [NSGraphicsContext restoreGraphicsState];
}

在第一次调用 drawRect 时,一切正常,绘图按照应有的方式进行,具体取决于 bAbajo 布尔值。之后,如果我再次调用 drawRect 与 bAbajo 相反的值,则会执行正确的代码(bAbajo 具有预期值,并且通过使用断点进行调试,我可以看到它正在 drawRect 中执行正确的代码),但窗口不会使用新的 NSBezierPath *path 更新。有什么帮助吗?

事实上,Rob 间接给了我答案,我在设置 bAbajo 后并没有调用 setNeedsDisplay。

相关内容

  • 没有找到相关文章

最新更新