我有一个显示图像的 NSView,我想让这个视图像裁剪图像效果一样。然后我制作3个矩形(imageRect
,secRect
和IntersectRect
),imageRect
是显示图像secRect
的矩形,只是使整体变暗的矩形imageRect
,并且intersectRect
是像观察矩形一样的矩形,我想做的是制作一个“洞" 上secRect
直接看到imageRect
(没有变暗)。这是我的 drawRect 方法:
- (void)drawRect:(NSRect)rect {
// Drawing code here.
NSImage *image = [NSImage imageNamed:@"Lonely_Tree_by_sican.jpg"];
NSRect imageRect = [self bounds];
[image compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver ];
if (NSIntersectsRect([myDrawRect currentRect], [self bounds])) {
//get the intersectionRect
intersectionRect = NSIntersectionRect([myDrawRect currentRect], imageRect);
//draw the imageRect
[image compositeToPoint:imageRect.origin operation:NSCompositeSourceOver];
//draw the secRect and fill it with black and alpha 0.5
NSRect secRect = NSMakeRect(imageRect.origin.x, imageRect.origin.y, imageRect.size.width, imageRect.size.height);
[[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.5] set];
[NSBezierPath fillRect:secRect];
//have no idea for the intersectRect
/*[image compositeToPoint:intersectionRect.origin
fromRect:secLayer
operation:NSCompositeXOR
fraction:1.0];*/
}
//draw the rectangle
[myDrawRect beginDrawing];
}
我有自己的类(myDrawRect)来根据鼠标点击绘制一个矩形[self bounds]
,所以只需忽略该beginDrawing
命令。
任何帮助都可以,谢谢。赫比。