zrs*_*slv 6 cocoa core-animation core-graphics calayer
我正在尝试使用以下方法绘制自定义形状的阴影CALayer:
#import <QuartzCore/QuartzCore.h>
@implementation ZKSBAppDelegate
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *view = self.window.contentView;
view.wantsLayer = YES;
CALayer *shadowLayer = [CALayer layer];
shadowLayer.shadowOpacity = 1;
shadowLayer.shadowRadius = 1;
shadowLayer.shadowOffset = NSMakeSize(0, 0);
CGMutablePathRef shadowPath = CGPathCreateMutable();
// setting the following rect's width to 100 fixes everything.
// setting it to 130 screws the shadow even more.
CGPathAddRect(shadowPath, NULL, CGRectMake(4, 0, 120, 48));
CGPathAddRect(shadowPath, NULL, CGRectMake(120, 50, 116, 48));
shadowLayer.shadowPath = shadowPath;
CGPathRelease(shadowPath);
[view.layer addSublayer:shadowLayer];
}
@end
Run Code Online (Sandbox Code Playgroud)
您可以在Xcode中创建一个空Cocoa项目,用上面的代码替换您的app.delegate的.m文件内容并自己尝试.
它看起来像特定的阴影路径几何导致CALayer疯狂.
例如:
CGPathAddRect(shadowPath, NULL, CGRectMake(4, 0, 100, 48));
CGPathAddRect(shadowPath, NULL, CGRectMake(120, 50, 116, 48));
Run Code Online (Sandbox Code Playgroud)
这个看起来完全没问题:

现在我要将第一个矩形扩大20点:
CGPathAddRect(shadowPath, NULL, CGRectMake(4, 0, 120, 48));
CGPathAddRect(shadowPath, NULL, CGRectMake(120, 50, 116, 48));
Run Code Online (Sandbox Code Playgroud)

......看起来不那么好了.加10分:
CGPathAddRect(shadowPath, NULL, CGRectMake(4, 0, 130, 48));
CGPathAddRect(shadowPath, NULL, CGRectMake(120, 50, 116, 48));
Run Code Online (Sandbox Code Playgroud)

现在这是完全错误的,不是吗?
所以,问题是:到底发生了什么,我做错了什么?你认为这是一个错误,我应该提交报告吗?