您的位置:首页 > 其它

Custom Views(Chapter 17 of Cocoa Programming for Mac OS X)

2011-02-24 11:17 761 查看
#import "StretchView.h"

@implementation StretchView

- (id)initWithFrame:(NSRect)rect
{
if (![super initWithFrame:rect])
{
return nil;
}

srandom(time(NULL));

path = [[NSBezierPath alloc] init];
[path setLineWidth:3.0];
NSPoint p = [self randomPoint];
[path moveToPoint:p];
int i;
for(i=0;i<15;i++)
{
p = [self randomPoint];
[path lineToPoint:p];
}
[path closePath];
return self;
}

- (void)dealloc
{
[path release];
[super dealloc];
}

- (NSPoint)randomPoint
{
NSPoint result;
NSRect r = [self bounds];
result.x = r.origin.x + random() % (int)r.size.width;
result.y = r.origin.y + random() % (int)r.size.height;
return result;
}

- (void)drawRect:(NSRect)dirtyRect
{
NSRect bounds = [self bounds];
[[NSColor greenColor] set];
[NSBezierPath fillRect:bounds];

[[NSColor whiteColor] set];
[path stroke];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: