• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSShadow with [bezierPath addClip]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSShadow with [bezierPath addClip]


  • Subject: Re: NSShadow with [bezierPath addClip]
  • From: Fritz Anderson <email@hidden>
  • Date: Tue, 31 Dec 2013 10:31:45 -0600

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


  • Follow-Ups:
    • Re: NSShadow with [bezierPath addClip]
      • From: Leonardo <email@hidden>
References: 
 >NSShadow with [bezierPath addClip] (From: Leonardo <email@hidden>)

  • Prev by Date: Re: NSView subclass does not seem to start
  • Next by Date: Re: NSView subclass does not seem to start
  • Previous by thread: NSShadow with [bezierPath addClip]
  • Next by thread: Re: NSShadow with [bezierPath addClip]
  • Index(es):
    • Date
    • Thread