2

我有一个带有跟踪区域的自定义视图类。我想要的是,当鼠标进入跟踪区域时,用颜色绘制贝塞尔曲线,当鼠标退出该区域时,贝塞尔曲线消失。为了让它消失,我读到唯一的方法是用窗口背景颜色改变它的颜色。

我设法添加了跟踪区域,但我不知道如何绘制贝塞尔曲线。如果我把代码放进去

-(void)drawRect:(NSRect)dirtyRect

它是在应用程序启动时绘制的,但我不希望这样。我试过这个:

@implementation MSBezier

- (void) viewWillMoveToWindow:(NSWindow *)newWindow {
    // Setup a new tracking area when the view is added to the window.
    NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:NSMakeRect(164.5, 17.5, 270, 65) options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void) mouseEntered:(NSEvent*)theEvent {
    NSLog(@"Entered");
    color = [NSColor colorWithCalibratedRed: 0.044 green: 0.813 blue: 0.044 alpha: 0.441];
    CGFloat rectangleCornerRadius = 31;
    NSRect rectangleRect = NSMakeRect(164.5, 17.5, 270, 65);
    NSRect rectangleInnerRect = NSInsetRect(rectangleRect, rectangleCornerRadius, rectangleCornerRadius);
    rectanglePath = NSBezierPath.bezierPath;
    [rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 180 endAngle: 270];
    [rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 270 endAngle: 360];
    [rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMaxY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 0 endAngle: 90];
    [rectanglePath lineToPoint: NSMakePoint(NSMinX(rectangleRect), NSMaxY(rectangleRect))];
    [rectanglePath closePath];
    [color setStroke];
    [rectanglePath setLineWidth: 3];
    [rectanglePath stroke];

}

- (void) mouseExited:(NSEvent*)theEvent {
    NSLog(@"Exited");
    color = [NSColor colorWithCalibratedRed: 0.949 green: 0.949 blue: 0.949 alpha: 1];
    CGFloat rectangleCornerRadius = 31;
    NSRect rectangleRect = NSMakeRect(164.5, 17.5, 270, 65);
    NSRect rectangleInnerRect = NSInsetRect(rectangleRect, rectangleCornerRadius, rectangleCornerRadius);
    rectanglePath = NSBezierPath.bezierPath;
    [rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 180 endAngle: 270];
    [rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 270 endAngle: 360];
    [rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMaxY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 0 endAngle: 90];
    [rectanglePath lineToPoint: NSMakePoint(NSMinX(rectangleRect), NSMaxY(rectangleRect))];
    [rectanglePath closePath];
    [color setStroke];
    [rectanglePath setLineWidth: 3];
    [rectanglePath stroke];
}

@end

但是没有绘制贝塞尔曲线。

谢谢你的帮助!

编辑 @uchuugaka

这是到目前为止的代码,它似乎没有做任何事情:

@implementation MSBezier

bool shouldDrawMyPath = YES;
NSBezierPath *rectanglePath;

- (void)viewWillDraw {

    if (shouldDrawMyPath == YES) {
        NSColor *color = [NSColor colorWithCalibratedRed: 0.044 green: 0.813 blue: 0.044 alpha: 0.441];
        CGFloat rectangleCornerRadius = 31;
        NSRect rectangleRect = NSMakeRect(164.5, 17.5, 270, 65);
        NSRect rectangleInnerRect = NSInsetRect(rectangleRect, rectangleCornerRadius, rectangleCornerRadius);
        rectanglePath = NSBezierPath.bezierPath;
        [rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 180 endAngle: 270];
        [rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 270 endAngle: 360];
        [rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMaxY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 0 endAngle: 90];
        [rectanglePath lineToPoint: NSMakePoint(NSMinX(rectangleRect), NSMaxY(rectangleRect))];
        [rectanglePath closePath];
        [color setStroke];
        [rectanglePath setLineWidth: 3];
    } else {
        rectanglePath = nil;
    }

}

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

    if (shouldDrawMyPath == YES) {
        [rectanglePath stroke];
    }
}

- (void) viewWillMoveToWindow:(NSWindow *)newWindow {
    NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:NSMakeRect(164.5, 17.5, 270, 65) options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void) mouseEntered:(NSEvent*)theEvent {
    NSLog(@"Entered");
    shouldDrawMyPath = YES;
    [self setNeedsDisplay:YES];
}

- (void) mouseExited:(NSEvent*)theEvent {
    NSLog(@"Exited");
    shouldDrawMyPath = NO;
    [self setNeedsDisplay:YES];
}

@end

我确定我做错了什么。

编辑 2

我只需要设置颜色drawRect:。所以:

-(void)drawRect:(NSRect)dirtyRect {
    if (shouldDrawMyPath == YES) {
        NSColor *color = [NSColor colorWithCalibratedRed: 0.044 green: 0.813 blue: 0.044 alpha: 0.441];
        [color setStroke];
        [rectanglePath stroke];
    }
}
4

1 回答 1

1

所以这很简单。你需要重新安排事情。首先创建一个像 shouldDrawMyPath 这样的 BOOL 属性,它应该默认为 NO。

接下来,在 mouseEntered 中:将其设置为 YES 调用 self setNeedsDisplay:YES

接下来,mouseExited:将其设置为 NO 调用 self setNeedsDisplay:YES

添加一个 NSBezierPath 属性。在 viewWillDraw 如果 shouldDrawMyPath == YES 设置你的贝塞尔路径。否则将其设置为 nil (我假设您的视图可能随时调整大小)如果您的视图从不调整大小,您可以创建一次路径。

在 drawRect 首先清除板,特别是如果您的视图可以调整大小。[[NSColor clearColor] 设置]; NSRectFill(self.bounds);

如果有的话,总是在那里画任何东西。

如果 shouldDrawMyPath == YES 填充和/或描边你的路径。Else 做任何其他没有路径的绘图。

只要您正确设置跟踪区域,这应该可以让您继续前进。

根据具体情况,总会有优化。

除非您知道自己在做什么,否则切勿在 drawRect 之外绘制。

保持你的 drawRect 代码简单。

仅在需要时绘制所需的内容。(您还没有到那里,但是可以进行很多优化以使无效并仅绘制特定的矩形)

于 2015-07-24T10:44:23.640 回答