我正在尝试创建一个带阴影的半圆 UIView。
我所做的是:
- 创建一个 UIView。
- 将其背景颜色设置为白色
- 设置 CornerRadius,使其成为圆形
使用 view.layer.shadow 属性为其添加阴影
circleView.backgroundColor = UIColor.whiteColor(); circleView.layer.cornerRadius = baseRoundView.bounds.height/2 ; circleView.layer.shadowColor = UIColor.blackColor().CGColor; circleView.layer.shadowOffset = CGSize(width: 0, height: -3); circleView.layer.shadowOpacity = 0.1; circleView.layer.shadowRadius = 1;
但是这些步骤只给了我一个带有阴影的完整圆形 UIVIEW。
所以我尝试遮盖下半部分
var maskRect: CGRect = CGRect(x: 0, y: 0), width: self.circleView.bounds.width, height: self.circleView.bounds.height/2);
var path = CGPathCreateWithRect(maskRect, nil);
maskLayer.path = path;
circleView.layer.mask = maskLayer;
这些步骤效果很好,只是我失去了顶部阴影
我期望得到什么
有什么想法吗?
最佳答案
如Larme pointed out in the comment ,我所要做的就是调整maskLayer的Y位置以覆盖阴影。
var maskRect: CGRect = CGRect(x: 0, y: -3), width: self.circleView.bounds.width, height: self.circleView.bounds.height/2 + 3 );
关于ios - 保留 mask UIView 的阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30666467/