我正在尝试使用以下方法绘制自定义形状的阴影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
你可以在 Xcode 中创建一个空的 Cocoa 项目,将你的 app.delegate 的 .m 文件内容替换成上面的代码,自己试试。
看起来特定的阴影路径几何会导致CALayer
发疯。
例如:
CGPathAddRect(shadowPath, NULL, CGRectMake(4, 0, 100, 48));
CGPathAddRect(shadowPath, NULL, CGRectMake(120, 50, 116, 48));
这个看起来完全没问题:
现在我要让第一个矩形宽 20 点:
CGPathAddRect(shadowPath, NULL, CGRectMake(4, 0, 120, 48));
CGPathAddRect(shadowPath, NULL, CGRectMake(120, 50, 116, 48));
……看起来不那么好了。加10分:
CGPathAddRect(shadowPath, NULL, CGRectMake(4, 0, 130, 48));
CGPathAddRect(shadowPath, NULL, CGRectMake(120, 50, 116, 48));
现在这是完全错误的,不是吗?
所以,问题是:到底发生了什么,我做错了什么吗?你认为这是一个错误,我应该提交报告吗?