使用Quartz绘制阴影在iPhone和iPad上很慢.其他方式?

Mod*_*ens 18 iphone shadow quartz-graphics uiview ipad

当我发现在iPhone/iPad上为我的UIViews添加阴影是多么容易时,我感到非常兴奋.

只需在Xcode中添加框架,将导入添加到文件顶部:

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)

然后呢:

self.contentView.layer.shadowRadius = 3.0;
self.contentView.layer.shadowOffset = CGSizeMake(-2.0, -3.0);
self.contentView.layer.shadowOpacity = 0.5;
self.contentView.layer.shadowColor = [UIColor blackColor].CGColor;
Run Code Online (Sandbox Code Playgroud)

虽然这确实在我的应用程序中创建了一个美丽的阴影,但是当显示视图时它现在也会落后于死...即使在调试器外部启动时也是如此.有没有我忘记的东西,或者这种方法对大型观点不实用?

作为参考,我在这里发布了一个截图.

cob*_*bal 53

你应该设置shadowPath属性.这是CoreGraphics能够优化阴影的方式.

例如,如果您的视图是不透明的矩形:

self.contentView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.contentView.bounds].CGPath;
Run Code Online (Sandbox Code Playgroud)

  • 我想为CALayer设置一个shadowPath,其中包含一个透明的图像.换句话说,不是矩形阴影,而是与源图像中的不透明度匹配的形状.有人知道这样做吗? (2认同)

Tha*_*all 6

以为我应该回答,因为我不希望这个宝石被埋没在评论中.

除了上面提到的cobbal的答案之外,occulus还提到了非矩形阴影的以下优化,例如文本阴影.

self.contentView.layer.shouldRasterize = YES;
// Fix visual degradation when rasterizing on high-density screens:
self.contentView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
Run Code Online (Sandbox Code Playgroud)