Cocoa NSView:制作圆圈,但它们被裁剪
我的圆在指南针每个点的外边缘都被裁剪(大概是通过矩形框)。如何让圆圈显示在框架内? (这是通过单击按钮创建的):
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你只看到一半的边缘,对吧?你可以计算边缘厚度的一半,然后从矩形中减去它:
我目前没有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:
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