Question

J'ai un NSBezierPath et je veux dessiner dans l'ombre d'encart (semblable à Photoshop) à l'intérieur du chemin.

Y at-il de toute façon de le faire? De plus, je sais que vous pouvez -stroke chemins, mais pouvez-vous la course à l'intérieur d'un chemin (similaire à l'intérieur des maladies dans Photoshop)?

Mise à jour 3

static NSImage * graydient = nil;

if (!graydient) {
    graydient = [[NSImage alloc] initWithSize: NSMakeSize(22, 22)];
    [graydient lockFocus];

    NSGradient * gradient = [[NSGradient alloc] initWithColorsAndLocations: clr(@"#262729"), 0.0f, clr(@"#37383a"), 0.43f, clr(@"#37383a"), 1.0f, nil];
    [gradient drawInRect: NSMakeRect(0, 4.179, 22, 13.578) angle: 90.0f];
    [gradient release];

    [graydient unlockFocus];
}

NSColor * gcolor = [NSColor colorWithPatternImage: graydient];

[gcolor set];

NSShadow * shadow = [NSShadow new];
[shadow setShadowColor: [NSColor colorWithDeviceWhite: 1.0f alpha: 1.0f]];
[shadow setShadowBlurRadius: 0.0f];
[shadow setShadowOffset: NSMakeSize(0, 1)];
[shadow set];

[path fill];

[NSGraphicsContext saveGraphicsState];

[[path pathFromIntersectionWithPath: [NSBezierPath bezierPathWithRect: NSInsetRect([path bounds], 0.6, 0)]] setClip];

[gcolor set];

[shadow setShadowOffset: NSMakeSize(0, 1)];
[shadow setShadowColor: [NSColor blackColor]];
[shadow set];

[outer stroke];

[NSGraphicsContext restoreGraphicsState];

[NSGraphicsContext saveGraphicsState];

[[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeSourceOut];

[shadow set];
[[NSColor whiteColor] set];
[inner fill];

[shadow set];
[inner fill];

[NSGraphicsContext restoreGraphicsState];

Résultat final Ceci est mon résultat final. Il semble assez bon. Je devais changer l'ombre White @ 1.0 Alpha pour le faire fonctionner. Même si la norme alpha de l'ombre pour les éléments de la barre de menu est de 0,5, il ne semble pas mal.

De nombreux remerciements à Joshua Nozzi .

Était-ce utile?

La solution

Je pense que vous pouvez le faire en réglant clip sur le chemin de Bézier pour l'utiliser comme un masque et caresser l'ombre, puis en ajoutant une course normale si on le souhaite.

Mise à jour à partir du code mis à jour:

Ici, vous allez. Je me sens ce soir-y remettre à plus tard. : -)

// Describe an inset rect (adjust for pixel border)
NSRect whiteRect = NSInsetRect([self bounds], 20, 20);
whiteRect.origin.x += 0.5;
whiteRect.origin.y += 0.5;

// Create and fill the shown path
NSBezierPath * path = [NSBezierPath bezierPathWithRect:whiteRect];
[[NSColor whiteColor] set];
[path fill];

// Save the graphics state for shadow
[NSGraphicsContext saveGraphicsState];

// Set the shown path as the clip
[path setClip];

// Create and stroke the shadow
NSShadow * shadow = [[[NSShadow alloc] init] autorelease];
[shadow setShadowColor:[NSColor redColor]];
[shadow setShadowBlurRadius:10.0];
[shadow set];
[path stroke];

// Restore the graphics state
[NSGraphicsContext restoreGraphicsState];

// Add a nice stroke for a border
[path setLineWidth:1.0];
[[NSColor grayColor] set];
[path stroke];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top