2009
10.29

In this tutorial we will be doing simple view programming by creating a very simple gradient view.

Sample code is attached below and if you want to download the complete XCode project, its attached below as well.

Optionally you could also add the angle property to it and synthesize it, to make the GradientView more customizable.

#import <cocoa /Cocoa.h>
 
@interface GradientView : NSView {
	NSColor *startColor;
	NSColor *endColor;
}
 
@property (nonatomic,retain) NSColor *startColor;
@property (nonatomic,retain) NSColor *endColor;
 
- (id) initWithStartColor:(NSColor *)startCol endColor:(NSColor *)endCol;
 
@end</cocoa>
#import "GradientView.h"
 
@implementation GradientView
 
@synthesize startColor;
@synthesize endColor;
 
/*
  Initilize the gradient view with colors
 */
- (id) initWithStartColor:(NSColor *)startCol endColor:(NSColor *)endCol {
    self = [super init];
	if (self) {
		self.startColor = startCol;
		self.endColor = endCol;
    }
    return self;
}
 
/*
  Draw the view with gradient
*/
- (void)drawRect:(NSRect)dirtyRect {
	NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:startColor endingColor:endColor];
	[gradient drawInRect:[self bounds] angle:270];
}
 
/*
  Dealloc resources
*/
- (void) dealloc {
	[startColor release];
	[endColor release];
	[super dealloc];
}
 
@end

Similar Posts:

File: CocoaGradient.zip (29 kB)
Caption: Cocoa Gradient XCode Project

No Comment.

Add Your Comment