Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 22, 2009, 09:31 PM   #1
GorillaPaws
macrumors 6502a
 
GorillaPaws's Avatar
 
Join Date: Oct 2003
Location: Richmond, VA
Problems getting an NSBezierPath to appear in a custom view

I'm trying to draw a few lines as individual NSBezierPath objects (I'm keeping them independant so that I can later do some core animation on them). I'm having issues getting the lines to appear, and I'm sure I'm missing something obvious. Here is my code for drawing my lineA:

Code:
- (id) initWithFrame: (NSRect) rect
{
	if (![super initWithFrame: rect])
		return nil;
	
	NSLog (@"drawing my custom view");
	NSRect bounds = [self bounds];
		
	//make the points
	NSPoint point1 = NSMakePoint ((bounds.origin.x) + 100, (bounds.origin.y) + 200);
	NSPoint point2 = NSMakePoint ((bounds.origin.x) + 100, (bounds.origin.y) + 50);
	
	//create the lines
	lineA = [[NSBezierPath alloc] init];
	[lineA setLineWidth: 3.0];
	[[NSColor blackColor] set];
	[lineA moveToPoint: point1];
	[lineA lineToPoint: point2];
	[lineA stroke];
	
	[self drawRect: rect];
	
	return self;
}
It builds fine although I'm getting CGContext errors on the console when my custom view tries to load. Also, I'm not seeing my 3pt wide vertical black line. I suspect this has something to do with the background (or lack-thereof) but any help/direction you could provide would be greatly appreciated.

Last edited by GorillaPaws; Jun 22, 2009 at 09:54 PM.
GorillaPaws is offline   0 Reply With Quote
Old Jun 22, 2009, 09:57 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
In an NSView you need to put your drawing code inside the drawRect: method, because this method gets called automatically by Cocoa when the view needs updating. You can tell the view to update via setNeedsDisplay:YES.

So it'd look something like:
Code:
- (void)drawRect:(NSRect)rect
{
	NSLog (@"drawing my custom view");
	NSRect bounds = [self bounds];
		
	//make the points
	NSPoint point1 = NSMakePoint ((bounds.origin.x) + 100, (bounds.origin.y) + 200);
	NSPoint point2 = NSMakePoint ((bounds.origin.x) + 100, (bounds.origin.y) + 50);
	
	//create the lines
	lineA = [[NSBezierPath alloc] init];
	[lineA setLineWidth: 3.0];
	[[NSColor blackColor] set];
	[lineA moveToPoint: point1];
	[lineA lineToPoint: point2];
	[lineA stroke];
}
kainjow is offline   0 Reply With Quote
Old Jun 22, 2009, 10:09 PM   #3
GorillaPaws
Thread Starter
macrumors 6502a
 
GorillaPaws's Avatar
 
Join Date: Oct 2003
Location: Richmond, VA
Worked beautifully in the drawRect: method. My only problem is that these lines will be changing and if I include this code in the drawRect method then I won't be able to customize their behavior or am I missing something?
GorillaPaws is offline   0 Reply With Quote
Old Jun 22, 2009, 10:21 PM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
If you're only drawing a few simple paths and not drawing very often, you don't need to cache the NSBezierPath objects. You can just create the paths directly each time in the drawRect: method based on whatever values you need.
kainjow is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Geektool: Problems creating my first custom script kiercardo UI Customization 3 Feb 5, 2014 05:24 AM
Anyone having problems with custom iTunes Radio stations? biosci Mac Applications and Mac App Store 26 Nov 10, 2013 02:52 PM
NSView and NSBezierPath - modelling free fall of an object moray95 Mac Programming 0 Mar 7, 2013 10:53 AM
Problems with Panorama View bromagz iOS 6 4 Sep 27, 2012 01:21 AM
NSButtons leave artefacts when used as subviews of a custom toolbar view demianturner Mac Programming 0 Sep 26, 2012 04:39 AM

Forum Jump

All times are GMT -5. The time now is 11:04 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC