I found this example of drawing a rectangle in objective-c cocoa
NSRect r = NSMakeRect(10, 10, 50, 60);
NSBezierPath *bp = [NSBezierPath bezierPathWithRect:r];
NSColor *color = [NSColor blueColor];
[color set];
[bp stroke];
However, where should I specify which NSView to draw on in the code?
For example, if I have two NSView objects, and I run this code, how do I specify which one to draw on?
You don't run this code on an NSView
, rather it runs in an NSView
subclass's override of the -drawRect:
method.
You'll need to create your own subclass of NSView
, then in that subclass, override -drawRect:
and put this code there:
@interface CustomView : NSView
@end
@implementation CustomView
- (void)drawRect:(NSRect)dirtyRect
{
NSRect r = NSMakeRect(10, 10, 50, 60);
NSBezierPath *bp = [NSBezierPath bezierPathWithRect:r];
NSColor *color = [NSColor blueColor];
[color set];
[bp stroke];
}
@end
Finally, you will of course have to instantiate an instance of CustomView
and add it to your view hierarchy, just as you would normally do with a regular NSView
.
子类NSView
,将代码添加到子NSView
的drawRect:
方法中,然后将适当的NSView
替换为NSView
的实例。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.