画像を表示する NSView があり、このビューをトリミング画像効果のように動作させたいと考えています。次に、3つの長方形(imageRect
、secRect
およびIntersectRect
)を作成しimageRect
ます。これは、画像を表示する四角形であり、secRect
全体を暗くするだけの四角形であり、四角形を観察するような四角形です。私がやりたいことは、「穴」を作るようなものです"上に直接(暗くせずに)見えるようにします。これが私の drawRect メソッドです:imageRect
intersectRect
secRect
imageRect
- (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
。
どんな助けでも大丈夫です、ありがとう。へビアン。