• 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: Hillegass Book (3rd Edition): Chapter 18 Challenge
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Hillegass Book (3rd Edition): Chapter 18 Challenge


  • Subject: Re: Hillegass Book (3rd Edition): Chapter 18 Challenge
  • From: Graham Cox <email@hidden>
  • Date: Tue, 27 May 2008 10:41:46 +1000

The oval you're dragging is being recreated and thrown away every time. Only the one actually added to the oval list on mouse up is ever drawn.

You need to change the code so that the oval is added on mouse down, then keep a reference to the current oval which you will need to clear and change its path on the drag event. The mouse up event won't need to do anything. When you mark the view for update in the drag, you need to mark it for both the old and the new oval path otherwise it'll leave trails as you drag it.

hope this helps,

Graham



On 27 May 2008, at 10:33 am, Grant Limberg wrote:

I'm going through Aaron HIllegass' new book and I'm having a bit of trouble on the first part of the Challenge in chapter 18. You're supposed to write a simple drawing app where the user can draw ovals on a custom view by clicking and dragging. I have it working for the most part except that the oval isn't actually displayed on the screen until after the mouseUp: event has happened. Any ideas on how to get it to draw while the mouse is being dragged? Here's some of the pertinent code. Thanks in advance.

-(void)drawRect:(NSRect)rect {
	NSLog(@"drawRect:");
	NSRect bounds = [self bounds];

	[[NSColor whiteColor] set];
	[NSBezierPath fillRect:bounds];

	//[currentPath stroke];
	[NSBezierPath strokeRect:[self currentRect]];

	for(NSBezierPath *oval in ovals) {
		[[NSColor blueColor] set];
		[oval stroke];
	}
}

-(void)addOval:(NSRect)rect {
	NSBezierPath *thePath =currentPath;
	[ovals addObject:thePath];
	[self setNeedsDisplay:YES];
}

#pragma mark Events

-(void)mouseDown:(NSEvent*)event {
	NSLog(@"mouseDown: %d", [event clickCount]);
	NSPoint p = [event locationInWindow];
	downPoint = [self convertPoint:p fromView:nil];
	currentPoint = downPoint;
	currentPath = [[NSBezierPath alloc] init];
	[self setNeedsDisplay:YES];
}

-(void)mouseDragged:(NSEvent*)event {
NSPoint p = [event locationInWindow];
NSLog(@"mouseDragged: %@", NSStringFromPoint(p));
currentPoint = [self convertPoint:p fromView:nil];
currentPath = [NSBezierPath bezierPathWithOvalInRect:[self currentRect]];
[self autoscroll:event];
[self setNeedsDisplay:YES];
}


-(void)mouseUp:(NSEvent*)event {
	NSLog(@"mouseUp:");
	NSPoint p = [event locationInWindow];
	currentPoint = [self convertPoint:p fromView:nil];
	[self addOval:[self currentRect]];
	[self setNeedsDisplay:YES];
}

-(NSRect)currentRect {
	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);

	return NSMakeRect(minX, minY, maxX-minX, maxY-minY);
}

Grant Limberg
email@hidden
LinkedIn: http://www.linkedin.com/in/grantlimberg
http://www.glsoftware.net

_______________________________________________

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

_______________________________________________

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


References: 
 >Hillegass Book (3rd Edition): Chapter 18 Challenge (From: Grant Limberg <email@hidden>)

  • Prev by Date: Hillegass Book (3rd Edition): Chapter 18 Challenge
  • Next by Date: Re: Hillegass Book (3rd Edition): Chapter 18 Challenge
  • Previous by thread: Hillegass Book (3rd Edition): Chapter 18 Challenge
  • Next by thread: Re: Hillegass Book (3rd Edition): Chapter 18 Challenge
  • Index(es):
    • Date
    • Thread