Cuadro de selección de dibujo (bandas de goma, hormigas marchando) en Cocoa, ObjectiveC
Actualmente, he implementado un cuadro de selección simple usando eventos del mouse y redibujando un rectángulo al arrastrar el mouse. Aquí está mi código:
-(void)drawRect:(NSRect)dirtyRect
{
if (!NSEqualRects(self.draggingBox, NSZeroRect))
{
[[NSColor grayColor] setStroke];
[[NSBezierPath bezierPathWithRect:self.draggingBox] stroke];
}
}
#pragma mark Mouse Events
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
self.draggingBox = NSMakeRect(pointInView.x, pointInView.y, 0, 0);
[self setNeedsDisplay:YES];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
_draggingBox.size.width = pointInView.x - (self.draggingBox.origin.x);
_draggingBox.size.height = pointInView.y - (self.draggingBox.origin.y);
[self setNeedsDisplay:YES];
}
- (void)mouseUp:(NSEvent *)theEvent
{
self.draggingBox = NSZeroRect;
[self setNeedsDisplay:YES];
}
Árbitro:http://cocoadev.com/HowToCreateWalkingAnts
Preguntas:
¿Es esta la forma más eficiente de hacer esto? Si la vista fuera compleja, sería más eficiente dibujar una vista transparente sobre la vista principal en lugar de volver a dibujar continuamente la vista durante el arrastre del mouse (http://www.cocoabuilder.com/archive/cocoa/99877-drawing-selection-rectangle.html)? ¿Cómo se hace esto? Parece que no puedo encontrar ningún ejemplo.