ios - 保留 mask UIView 的阴影

标签 ios swift uiview custom-controls calayer

我正在尝试创建一个带阴影的半圆 UIView。

我所做的是:

  1. 创建一个 UIView。
  2. 将其背景颜色设置为白色
  3. 设置 CornerRadius,使其成为圆形
  4. 使用 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。

Full Circle

所以我尝试遮盖下半部分

    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;

这些步骤效果很好,只是我失去了顶部阴影 issing Shadow

我期望得到什么

Expectation

有什么想法吗?

最佳答案

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/

相关文章:

ios - SpriteKit : character movement set by draging him across the screen ( x axis) but stay at a set ( y position)

ios - ios中的程序流程问题

ios - UIView,非常缓慢地动画背景颜色,挂起 ScrollView (!)

swift - Xcode 中的按钮突出显示

ios - LazyVGrid 中的 SwiftUI NavigationLink 选择随机索引

ios - 使用负速度恢复 CABasicAnimation?

ios - 使用父 View 管理另一个 UIView 导出

ios - 使用与当前设备语言不同的系统 View 语言

iOS xcode 按钮未链接。

ios - 添加到 UITableView 导航栏下方的 subview Y 值不正确