【问题标题】:Preserve Shadow of a Masked UIView保留蒙面 UIView 的阴影
【发布时间】:2015-06-05 12:21:37
【问题描述】:

我正在尝试创建一个带阴影的半圆形 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;
    

    但是这些步骤只给了我一个带阴影的 FULL CIRCLE 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;

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

我期望拥有的东西

有什么想法吗?

【问题讨论】:

  • 阴影在y上的偏移量为-3。所以如果我没记错的话,它会扩展实际大小(clipsToBounds 是false)。所以你也必须在面具上做-3(并且可能在高度上做+3)。
  • 谢谢拉姆,这正是我所需要的!用你的答案更新了问题

标签: ios swift uiview custom-controls calayer


【解决方案1】:

作为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  );

【讨论】:

    猜你喜欢
    • 2020-01-11
    • 2018-05-19
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多