我正在尝试使用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));
现在这是完全错的,不是吗?
所以,问题是:到底发生了什么,我做错了什么?你认为这是一个错误,我应该提交报告吗?
答案 0 :(得分:3)
是的,这听起来像个bug。即使不是,通过提交错误报告,Apple应该回复并提供有用的信息。