Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 22, 2009, 08: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 08:54 PM.
GorillaPaws is offline   0 Reply With Quote
Old Jun 22, 2009, 08: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, 09: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, 09: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Can't Get Windows Shared Files to Appear in Mac T3hPreacher Windows, Linux & Others on the Mac 0 Jul 25, 2011 09:26 AM
Get all relevant services to appear in context menus? devburke OS X 9 Sep 8, 2009 11:16 PM
Hold down links to get an option to open in new window? stateofmind iPhone 6 May 16, 2009 04:46 AM
Getting an iPhone to use in Israel bobobo iPhone 3 Nov 15, 2007 08:11 AM
How do I get external drive icons to appear in the same place??? danielbriggs OS X 1 Jun 12, 2007 01:35 PM


All times are GMT -5. The time now is 01:02 PM.

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

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