extra redrawing with [self setBounds:] in drawRect:
extra redrawing with [self setBounds:] in drawRect:
- Subject: extra redrawing with [self setBounds:] in drawRect:
- From: Nathan Vander Wilt <email@hidden>
- Date: Mon, 28 Jan 2008 19:43:01 -0600 (CST)
extra redrawing with [self setBounds:] in drawRect:
In my custom view, I set the bounds in my drawRect:
method before drawing. This is so that while the user
is resizing the window, the content stretches with the
view. I then added some code to draw a selection
rectangle as the user drags across the image.
Somehow this selection rectangle code triggers random
extra redrawing. As soon as I start dragging things go
weird. Dragging itself causes the whole view to
flicker, instead of just the invalidated portions.
Even when done dragging, the view keeps getting pairs
of drawRect calls *way* more often than normal: any
time the window is brought to front, any time the
window as a whole is moved, etc. If I resize the
window the spurious events stop, until I drag within
the view again.
If I call setBounds: only if ([self inLiveResize]),
then the problem goes away and the view only gets
asked to redraw when dragging or window resizing
invalidates it. But by my understanding, setBounds:
shouldn't cause extra redrawing in the first place, so
I don't think I've gotten to the root of the problem.
Is it kosher to call [self setBounds:] in drawRect:?
Why would a combination of setNeedsDisplayInRect: in
an event responder and setBounds: in the draw code
lead to the view getting extra drawRect: calls even
when the event responders are done being used?
thanks,
-natevw
Here's the relevant code (critique of any aspect
appreciated as well):
@interface CTPrototypeMapView : NSView {
@private
NSArray* polygons;
BOOL dragging;
NSPoint startingDragLocation;
NSPoint currentDragLocation;
}
@end
NSRect CTMakeRectFromPoints(NSPoint a, NSPoint b) {
CGFloat origin_x, origin_y;
CGFloat width, height;
if (a.x < b.x) {
origin_x = a.x;
width = b.x - a.x;
} else {
origin_x = b.x;
width = a.x - b.x;
}
if (a.y < b.y) {
origin_y = a.y;
height = b.y - a.y;
} else {
origin_y = b.y;
height = a.y - b.y;
}
return NSMakeRect(origin_x, origin_y, width, height);
}
@implementation CTPrototypeMapView
- (void)invalidateCurrentDragArea {
NSRect dragRect =
CTMakeRectFromPoints(startingDragLocation,
currentDragLocation);
//NSRect marredRect = NSInsetRect(dragRect, -0.2,
-0.2); // stroke extends a bit beyond passed rectangle
NSRect marredRect = dragRect;
[self setNeedsDisplayInRect: marredRect];
}
- (void)drawRect:(NSRect)rect {
[self setBounds:NSMakeRect(-180, -90, 360, 180)];
// ...draw a bunch of shapes...
if (dragging) {
NSRect dragRectangle =
CTMakeRectFromPoints(startingDragLocation,
currentDragLocation);
NSBezierPath* shapeForDrag = [NSBezierPath
bezierPathWithRoundedRect:dragRectangle xRadius:4
yRadius:4];
[shapeForDrag setLineWidth:0.2];
[shapeForDrag stroke];
}
}
- (void)dragTo:(NSPoint)location {
[self invalidateCurrentDragArea];
currentDragLocation = location;
[self invalidateCurrentDragArea];
}
- (void)mouseDown:(NSEvent*)event {
dragging = YES;
NSPoint event_coordinates = [event locationInWindow];
startingDragLocation = [self
convertPoint:event_coordinates fromView:nil];
}
- (void)mouseDragged:(NSEvent*)event {
if (!dragging) return;
NSPoint event_coordinates = [event locationInWindow];
NSPoint newDragLocation = [self
convertPoint:event_coordinates fromView:nil];
[self dragTo:newDragLocation];
}
- (void)mouseUp:(NSEvent*)event {
dragging = NO;
[self invalidateCurrentDragArea];
}
@end
____________________________________________________________________________________
¡Capacidad ilimitada de almacenamiento en tu correo!
No te preocupes más por el espacio de tu cuenta con Correo Yahoo!:
http://correo.espanol.yahoo.com/
_______________________________________________
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