0

我有一个关于在NSView. NSRect 何时使用类struct ( drawrect) 与使用CoreGraphics框架是否有“规则” ?到目前为止,我所了解的我可以将任何一个用于我的 osx 应用程序。我可以吗?

4

1 回答 1

2

首先,没有NSRect (它是一个结构),您指的是NSViewmethod drawRect:。一般来说,Core Graphics 框架比 AppKit 绘图方法“低级”,AppKit 通常只是围绕 Core Graphics 的“包装器”。它也是纯 C 而不是 Objective-C,因此 AppKit 通常更易于使用,但您可以在两者之间自由混合和匹配。

例如:

- (void)drawRect:(NSRect)rect
{
    //This uses AppKit:
    [[NSColor redColor] set];
    [[NSBezierPath bezierPathWithOvalInRect:[self bounds]] fill];

    //This does the same, using Core Graphics:
    CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
    CGContextSetRGBFillColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextFillEllipseInRect(ctx, NSRectToCGRect([self bounds]));
}
于 2013-03-19T12:15:34.697 回答