0

我有 2 个 bezierPath。在第一条路径上,我绘制网格。我处理鼠标事件并在第一条路径上绘制新路径。我使用 appendBezierPath,没关系,但是第二条路径的样式不复制。Cocoa 使用第一个路径样式。如何以新样式附加新的贝塞尔路径。例如,我绘制黑色网格,我想在第二条路径中绘制红色圆圈。

升级版:

- (void)drawRect:(NSRect)dirtyRect {
    NSRect bounds = [self bounds];
    NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
    [backgroundColor set];
    NSRectFill(bounds);

    [lineColor set];
    for(NSUInteger i = 30; i < NSMaxX(bounds); i += 60) {
        [path moveToPoint: (NSPoint) {i, 0}];
        [path lineToPoint: (NSPoint) {i, NSMaxY(bounds)}];
        for(NSUInteger j = 30; j < NSMaxY(bounds); j += 60) {
            [path moveToPoint: (NSPoint) {0, j}];
            [path lineToPoint: (NSPoint) {NSMaxX(bounds), j}];
        }
    }
    [NSGraphicsContext saveGraphicsState];
    [path stroke];
    [currentContext restoreGraphicsState];
}

- (void)setDeniedPoint:(NSPoint)point {
    NSBezierPath *newPath = [NSBezierPath bezierPath];
    [deniedPointColor set];
    [newPath setLineWidth:3.0f];
    [newPath moveToPoint:(NSPoint) {point.x-4, point.y-4}];
    [newPath lineToPoint:(NSPoint) {point.x+4, point.y+4}];
    [newPath moveToPoint:(NSPoint) {point.x-4, point.y+4}];
    [newPath lineToPoint:(NSPoint) {point.x+4, point.y-4}];
    [path appendBezierPath:newPath];
    [self setNeedsDisplay:YES];
}

- (void)mouseDown:(NSEvent *)event {
    NSPoint currentLocationCursor = [self convertPoint:[event locationInWindow] fromView:nil];
    [self setDeniedPoint:currentLocationCursor];
}
4

0 回答 0