【发布时间】:2013-12-03 17:38:05
【问题描述】:
我目前使用鼠标事件实现了一个简单的选择框,并在鼠标拖动时重绘了一个矩形。这是我的代码:
-(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];
}
参考:http://cocoadev.com/HowToCreateWalkingAnts
问题:
这是最有效的方法吗?如果视图很复杂,在主视图上绘制透明视图而不是在鼠标拖动期间不断重绘视图会更有效吗(http://www.cocoabuilder.com/archive/cocoa/99877-drawing-selection-rectangle.html)?这是怎么做到的?我似乎找不到任何例子。
【问题讨论】:
标签: objective-c macos cocoa nsview