Hillegass Book (3rd Edition): Chapter 18 Challenge
Hillegass Book (3rd Edition): Chapter 18 Challenge
- Subject: Hillegass Book (3rd Edition): Chapter 18 Challenge
- From: Grant Limberg <email@hidden>
- Date: Mon, 26 May 2008 17:33:12 -0700
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