Cocoa NSView:制作圆圈,但它们被裁剪

发布于 2024-10-16 18:49:05 字数 920 浏览 8 评论 0原文

我的圆在指南针每个点的外边缘都被裁剪(大概是通过矩形框)。如何让圆圈显示在框架内? (这是通过单击按钮创建的):

在我的 AppController.m 中

#import "AppController.h"
#import "MakeCircle.h"

@implementation AppController

- (IBAction)makeCircle:(id)sender {

     MakeCircle* newCircle = [[MakeCircle alloc] initWithFrame:NSMakeRect(100.0, 100.0, 30.0, 30.0)];
     [[[[NSApplication sharedApplication] mainWindow] contentView] addSubview:newCircle];

     [newCircle release];
}

@end

在我的 MakeCircle.m 中

- (void)drawRect:(NSRect)rect {

     [self setNeedsDisplay:YES];

     [[NSColor blackColor] setStroke];

     // Create our circle path
     NSBezierPath* circlePath = [NSBezierPath bezierPath];
     [circlePath appendBezierPathWithOvalInRect: rect];

     //give the line some thickness
     [circlePath setLineWidth:4];

     // Outline and fill the path
     [circlePath stroke];


  }

谢谢。

the outer edges of my circle at each point of the compass are getting cropped (presumably by the rect frame). How do I get the circle to display within the frame? (This is getting created from a button click):

In my AppController.m

#import "AppController.h"
#import "MakeCircle.h"

@implementation AppController

- (IBAction)makeCircle:(id)sender {

     MakeCircle* newCircle = [[MakeCircle alloc] initWithFrame:NSMakeRect(100.0, 100.0, 30.0, 30.0)];
     [[[[NSApplication sharedApplication] mainWindow] contentView] addSubview:newCircle];

     [newCircle release];
}

@end

In my MakeCircle.m

- (void)drawRect:(NSRect)rect {

     [self setNeedsDisplay:YES];

     [[NSColor blackColor] setStroke];

     // Create our circle path
     NSBezierPath* circlePath = [NSBezierPath bezierPath];
     [circlePath appendBezierPathWithOvalInRect: rect];

     //give the line some thickness
     [circlePath setLineWidth:4];

     // Outline and fill the path
     [circlePath stroke];


  }

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-10-23 18:49:05

我想你只看到一半的边缘,对吧?你可以计算边缘厚度的一半,然后从矩形中减去它:

#define STROKE_COLOR    ([NSColor blackColor])
#define STROKE_WIDTH    (4.0)
- (void)drawRect:(NSRect)dirtyRect {
    NSBezierPath *path;
    NSRect rectangle;

    /* Calculate rectangle */
    rectangle = [self bounds];
    rectangle.origin.x += STROKE_WIDTH / 2.0;
    rectangle.origin.y += STROKE_WIDTH / 2.0;
    rectangle.size.width -= STROKE_WIDTH / 2.0;
    rectangle.size.height -= STROKE_WIDTH / 2.0;
    path = [NSBezierPath path];
    [path appendBezierPathWithOvalInRect:rectangle];
    [path setLineWidth:STROKE_WIDTH];
    [STROKE_COLOR setStroke];
    [path stroke];
}

我目前没有Mac,所以我无法测试它,但我认为它应该可以解决你的问题。

也不要调用[self setNeedsDisplay:YES]。当你想要重绘整个 NSView 时使用该方法,并且从绘图方法调用它有点递归。这就是为什么我对你的代码实际上绘制了一些东西感到惊讶。

我还有另一个提示:[[NSApplication sharedApplication] mainWindow] 实际上与 [NSApp mainWindow] 相同。 NSApp 是一个包含主应用程序的全局变量。

希望有帮助,
IEF2

I think you see only half of the edge, right? You can calculate the half of the thickness of the edge and subtract that from the rectangle:

#define STROKE_COLOR    ([NSColor blackColor])
#define STROKE_WIDTH    (4.0)
- (void)drawRect:(NSRect)dirtyRect {
    NSBezierPath *path;
    NSRect rectangle;

    /* Calculate rectangle */
    rectangle = [self bounds];
    rectangle.origin.x += STROKE_WIDTH / 2.0;
    rectangle.origin.y += STROKE_WIDTH / 2.0;
    rectangle.size.width -= STROKE_WIDTH / 2.0;
    rectangle.size.height -= STROKE_WIDTH / 2.0;
    path = [NSBezierPath path];
    [path appendBezierPathWithOvalInRect:rectangle];
    [path setLineWidth:STROKE_WIDTH];
    [STROKE_COLOR setStroke];
    [path stroke];
}

I have no Mac at the moment, so I can't test it, but I think it should solve your problem.

Als don't call [self setNeedsDisplay:YES]. The method is used when you want to redraw your whole NSView, and calling it from the drawing method is a little bit recursive. That's why I'm surprised your code actually draws something.

And I have another tip: [[NSApplication sharedApplication] mainWindow] is actually the same as [NSApp mainWindow]. NSApp is a global variable containing the main application.

Hope it helps,
ief2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文