可可 – 如何绘制模糊的形状?

可可 – 如何绘制模糊的形状?,第1张

概述如何在 Cocoa中绘制模糊的形状?可以想象一个带有blurRadius的阴影,它伴随着一条填充的路径,但没有锐利的前景路径形状. 我尝试的是使用带阴影的填充路径,并将填充颜色设置为透明(alpha 0.0).但这也使阴影也不可见,因为它显然将阴影投射“对象”的alpha考虑在内. 这实际上相当棘手.我挣扎了一段时间,直到我在NSShadow上提出这个类别: @implementation NSS @H_301_0@ @H_301_0@ 如何在 Cocoa中绘制模糊的形状?可以想象一个带有blurRadius的阴影,它伴随着一条填充的路径,但没有锐利的前景路径形状.

@H_502_8@

我尝试的是使用带阴影的填充路径,并将填充颜色设置为透明(Alpha 0.0).但这也使阴影也不可见,因为它显然将阴影投射“对象”的Alpha考虑在内.@H_502_8@解决方法 这实际上相当棘手.我挣扎了一段时间,直到我在NSShadow上提出这个类别:

@H_502_8@

@H_502_8@

@implementation NSShadow (Extras)//draw a shadow using a bezIEr path but do not draw the bezIEr path- (voID)drawUsingBezIErPath:(NSBezIErPath*) path Alpha:(CGfloat) Alpha{    [NSGraphicsContext saveGraphicsstate];    //get the bounds of the path    NSRect bounds = [path bounds];    //create a rectangle that outsets the size of the path bounds by the blur radius amount    CGfloat blurRadius = [self shadowBlurRadius];    NSRect shadowBounds = NSInsetRect(bounds,-blurRadius,-blurRadius);    //create an image to hold the shadow    NSImage* shadowImage = [[NSImage alloc] initWithSize:shadowBounds.size];    //make a copy of the shadow and set its offset so that when the path is drawn,the shadow is drawn in the mIDdle of the image    NSShadow* aShadow = [self copy];    [aShadow setShadowOffset:NSMakeSize(0,-NSHeight(shadowBounds))];    //lock focus on the image    [shadowImage lockFocus];    //we want to draw the path directly above the shadow image and offset the shadow so it is drawn in the image rect    //to do this we must translate the drawing into the correct location    NSAffinetransform* transform=[NSAffinetransform transform];    //first get it to the zero point    [transform translateXBy:-shadowBounds.origin.x yBy:-shadowBounds.origin.y];    //Now translate it by the height of the image so that it draws outsIDe the image bounds    [transform translateXBy:0.0 yBy:NSHeight(shadowBounds)];    NSBezIErPath* translatedpath = [transform transformBezIErPath:path];    //apply the shadow    [aShadow set];    //fill the path with an arbitrary black color    [[NScolor blackcolor] set];    [translatedpath fill];    [aShadow release];    [shadowImage unlockFocus];    //draw the image at the correct location relative to the original path    NSPoint imageOrigin = bounds.origin;    imageOrigin.x = (imageOrigin.x - blurRadius) + [self shadowOffset].wIDth;    imageOrigin.y = (imageOrigin.y - blurRadius) - [self shadowOffset].height;    [shadowImage drawAtPoint:imageOrigin fromrect:NSZeroRect operation:NSCompositeSourceOver fraction:Alpha];    [shadowImage release];    [NSGraphicsContext restoreGraphicsstate];}@end
@H_301_0@ 总结

以上是内存溢出为你收集整理的可可 – 如何绘制模糊的形状?全部内容,希望文章能够帮你解决可可 – 如何绘制模糊的形状?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1005485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存