Re: NSShadow with [bezierPath addClip]
Re: NSShadow with [bezierPath addClip]
- Subject: Re: NSShadow with [bezierPath addClip]
- From: Leonardo <email@hidden>
- Date: Tue, 31 Dec 2013 20:37:21 +0100
- Thread-topic: NSShadow with [bezierPath addClip]
Thank you Fritz. I am afraid your method doesn't work in my case.
Me too I need to draw a rounded rect path with shadow. But its background
has an half-a-transparent color. So if I "stroke" the bezierPath (to cast
the shadow) I can quite see the path as a line. If I "fill" the bezierPath
(to cast the shadow) I mix the whiteColor (or any other) with the
background color. And I cannot cast a shadow filling a path with a
clearColor.
I am puzzled.
Regards
-- Leonardo
> Da: Fritz Anderson <email@hidden>
> Data: Tue, 31 Dec 2013 10:31:45 -0600
> A: Leonardo <email@hidden>
> Cc: Cocoa Developers <email@hidden>
> Oggetto: Re: NSShadow with [bezierPath addClip]
>
> On 31 Dec 2013, at 6:15 AM, Leonardo <email@hidden> wrote:
>
>> In my NSView subclass, I have to draw an image within a bezierPath and apply
>> an NSShadow to the result.
>> So within the drawRect: method I add
>>
>> [bezierPath addClip];
>> [mImage drawInRect:inRect fromRect:fromRect
>> operation:NSCompositeSourceOver fraction:mOpacity];
>>
>> It works. The image gets clipped by the path.
>> Now I would like to apply the NSShadow to the result, so before those lines
>> above I add
>>
>> [bezierPath addClip];
>> [mShadow set];
>> [mImage drawInRect:inRect fromRect:fromRect operation:NSCompositeSourceOver
>> fraction:mOpacity];
>>
>> I can't get the shadow drawn because the bezierPath is clipping the drawing
>> area where the shadow should fall.
>> How to solve this trouble?
>> I have tried to use a layer, but I get weird results when I resize, rotate
>> and modify other parameters of the view.
>
> This worked for me:
>
> ====
> - (void) drawRect:(NSRect)dirtyRect
> {
> NSBezierPath * bezierPath = [[NSBezierPath alloc] init];
> [bezierPath appendBezierPathWithRoundedRect: self.clipPathRect
> xRadius: 10.0 yRadius: 10.0];
>
> [bezierPath setLineWidth: 0.0];
>
> NSGraphicsContext * context = [NSGraphicsContext
> graphicsContextWithWindow:
> self.window];
>
> // Clear the view
> [context saveGraphicsState]; {
> [[NSColor whiteColor] set];
> [NSBezierPath fillRect: dirtyRect];
> } [context restoreGraphicsState];
>
> // Stroke the (zero-width) clipping path
> [context saveGraphicsState]; {
> [self.shadow set];
> [bezierPath stroke];
> // No width means no visible boundary line,
> // but the shadow is still drawn.
> } [context restoreGraphicsState];
>
> // Draw the image, clipped to the path
> [context saveGraphicsState]; {
> [bezierPath addClip];
> [_image drawInRect: self.clipPathRect
> fromRect: self.selectedSourceRect
> operation: NSCompositeSourceOver
> fraction: self.imageFraction];
> // The image obscures the inner shadow.
> // Bug: The shadow shows through to the extent imageFraction < 1.
> // Can the shadow drawing be clipped to the inverse of the clipping
> path?
> } [context restoreGraphicsState];
> }
> ====
>
> F
>
_______________________________________________
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