iOS:如何创建单边阴影效果

9
我有以下模仿Facebook的菜单样式布局。我想要在左侧加上阴影,就像下面这样,但是我使用的带有层阴影的代码会导致应用程序卡顿。我一直没有找到一个好的替代解决方案。有没有人有不影响应用程序性能的创建阴影的替代方法?
[self.navController.view.layer setShadowOffset:CGSizeMake(0, 1)];
[self.navController.view.layer setShadowColor:[[UIColor darkGrayColor] CGColor]];
[self.navController.view.layer setShadowRadius:8.0];
[self.navController.view.layer setShadowOpacity:0.8];

上面的代码是我目前使用的代码,会导致应用程序性能问题。
下面的图片是我想要实现的,但不想使用上面的代码。 enter image description here
3个回答

11

最好指定一个阴影路径,例如:

CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:self.navController.view.layer.bounds].CGPath;
[self.navController.view.layer setShadowPath:shadowPath]

根据CALayer文档,“明确指定路径通常会提高渲染性能。”


太棒了!这确实帮了很多,不过我还是注意到了一点性能问题,但并不太严重。 - Bot

4
您可以直接插入视图的边界,并设置阴影路径:
UIEdgeInsets contentInsets = UIEdgeInsetsMake(10, 0, 0, 0);
CGRect shadowPathExcludingTop = UIEdgeInsetsInsetRect(self.bounds, contentInsets);
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:shadowPathExcludingTop].CGPath;

0
我个人会添加一个包含阴影的UIView作为子视图,并在右侧视图动画时重新定位它。(例如,使用层或核心图形绘制该阴影)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接