• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [NSBezierPath fillRect:[self bounds]] question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [NSBezierPath fillRect:[self bounds]] question


  • Subject: Re: [NSBezierPath fillRect:[self bounds]] question
  • From: Rob Keniger <email@hidden>
  • Date: Wed, 27 Feb 2008 11:55:05 +1000


On 27/02/2008, at 11:05 AM, Jamie Phelps wrote:

Hey, guys. I'm having what I assume is a simple problem. [NSBezierPath fillRect:[self bounds]]; fills my NSView subclass fine the first time drawRect: is called, but breaks the frame on redraw. Here is some more code for context if needed. I tried adding the initial background fill to an awakeFromNib: method with no success. If anyone can offer thoughts on where I'm going wrong I'd appreciate it.


There are several major problems with your code. You need to go back and read the Cocoa Drawing Guide and make sure you understand it. You should probably also have a look at the Sketch example project.

You cannot call -setNeedsDisplay: in your -drawRect: method. -drawRect is called automatically at the end of the event loop if the view is marked as needing display and setting -setNeedsDisplay: in the drawRect makes no sense at all.

In your -mouseUp: method, you are directly calling -drawRect: without locking focus on anything - this is not permitted and will cause massive problems. You should never call -drawRect: directly, just call -setNeedsDisplay:YES and -drawRect: will be called automatically.

I also don't see why you're calling -setNeedsDisplay on mouse down, since there is no change in what is to be drawn.

You also don't initialize your ovalBounds NSRect or test to see whether it is valid, which would be good practice.

I have made a few changes to your code, including using -mouseDragged: to handle the oval drawing instead of -mouseUp: and I think it achieves what you are probably looking for.

@implementation DrawingView
- (id)initWithFrame:(NSRect)rect{
	self = [super initWithFrame:rect];
	[NSBezierPath setDefaultLineWidth:5.0];
	//initialize the ovalBounds
	ovalBounds=NSZeroRect;
	return self;
}
- (void)drawRect:(NSRect)rect{
	[[NSColor blueColor] set];
	[NSBezierPath fillRect:[self bounds]];
	//test to ensure that the oval is valid
	if(!NSEqualRects(NSZeroRect, ovalBounds))
	{
		path = [NSBezierPath bezierPathWithOvalInRect:ovalBounds];
		[[NSColor yellowColor] set];
		[path stroke];
		[[NSColor greenColor] set];
		[path fill];
	}
}
//in mouseDown we just set the origin point
- (void)mouseDown:(NSEvent *)event{
	NSPoint p = [event locationInWindow];
	downPoint = [self convertPoint:p
						  fromView:nil];
}

-(void) mouseDragged:(NSEvent*) event
{
	NSPoint p = [event locationInWindow];
	currentPoint = [self convertPoint:p
						  fromView:nil];
	float minX = MIN(downPoint.x, currentPoint.x);
	float maxX = MAX(downPoint.x, currentPoint.x);
	float minY = MIN(downPoint.y, currentPoint.y);
	float maxY = MAX(downPoint.y, currentPoint.y);
	ovalBounds = NSMakeRect(minX, minY, maxX-minX, maxY-minY);
	[self setNeedsDisplay:YES];
}
@end

--
Rob Keniger
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: [NSBezierPath fillRect:[self bounds]] question
      • From: Jamie Phelps <email@hidden>
    • Re: [NSBezierPath fillRect:[self bounds]] question
      • From: Jamie Phelps <email@hidden>
References: 
 >[NSBezierPath fillRect:[self bounds]] question (From: Jamie Phelps <email@hidden>)

  • Prev by Date: how to know current page?
  • Next by Date: Re: Can CoreData return only unique results of an attribute
  • Previous by thread: [NSBezierPath fillRect:[self bounds]] question
  • Next by thread: Re: [NSBezierPath fillRect:[self bounds]] question
  • Index(es):
    • Date
    • Thread