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
@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
相关文章推荐
- Objective-C(Chapter 3 of Cocoa Programming for Mac OS X)
- Key-Value Coding, Key-Value observing(Chapter 7 of Cocoa Programming for Mac OS X)
- Basic Core Data(Chapter 11 of Cocoa Programming for Mac OS X)
- Printing(Chapter 27 of Cocoa Programming for Mac OS X)
- Keyboard Events(Chapter 19 of Cocoa Programming for Mac OS X)
- User Defaults(Chapter 13 of Cocoa Programming for Mac OS X)
- Using Alert Panels(Chapter 15 of Cocoa Programming for Mac OS X)
- Localization(Chapter 16 of Cocoa Programming for Mac OS X)
- Drawing Text With Attributes(Chapter 20 of Cocoa Programming for Mac OS X)
- Pasteboards and Nil-Targeted Actions(Chapter 21 of Cocoa Programming for Mac OS X)
- NSTimer(Chapter 24 of Cocoa Programming for Mac OS X)
- Target/Action(Chapter 5 of Cocoa Programming for Mac OS X)
- NSUndoManager(Chapter 9 of Cocoa Programming for Mac OS X)
- Sheets(Chapter 25 of Cocoa Programming for Mac OS X)
- Core Data Relationships(Chapter 30 of Cocoa Programming for Mac OS X)
- Helper Objects (Chapter 6 of Cocoa Programming for Mac OS X)
- Creating NSFormatters(Chapter 26 of Cocoa Programming for Mac OS X)
- Web Service(Chapter 28 of Cocoa Programming for Mac OS X)
- Archiving(Chapter 10 of Cocoa Programming for Mac OS X)
- Nib Files and NSWindowController(Chapter 12 of Cocoa Programming for Mac OS X)