【问题标题】:How to mask and add shadow to a UIView如何为 UIView 遮罩和添加阴影
【发布时间】:2016-09-30 18:19:10
【问题描述】:

我正在制作一个我想要遮罩并为其添加阴影的自定义视图。

掩蔽:

    let p = UIBezierPath()
    p.moveToPoint(CGPointMake(20, 20))
    p.addLineToPoint(CGPointMake(100, 20))
    p.addLineToPoint(CGPointMake(100, 50))
    p.addLineToPoint(CGPointMake(110, 55))
    p.addLineToPoint(CGPointMake(100, 60))
    p.addLineToPoint(CGPointMake(100, 100))
    p.addLineToPoint(CGPointMake(20, 100))
    p.closePath()

    let s = CAShapeLayer()
    s.frame = layer.bounds
    s.path = p.CGPath
    s.fillColor = UIColor.greenColor().CGColor
    layer.mask = s

掩蔽工作,现在我想添加一个阴影。 但它不起作用。

我尝试在主图层上添加阴影,但没有任何反应。

    layer.shadowColor = UIColor.yellowColor().CGColor
    layer.shadowRadius = 10
    layer.shadowOpacity = 0.9
    layer.shadowOffset = CGSizeZero

我尝试将它添加到遮罩层,但我得到了用阴影遮罩的主视图。

    s.shadowColor = UIColor.yellowColor().CGColor
    s.shadowRadius = 10
    s.shadowOpacity = 0.9
    s.shadowOffset = CGSizeZero

任何建议如何将此黄色阴影添加到蒙版视图?

谢谢

【问题讨论】:

  • 确保您的视图没有零帧
  • 它没有,我可以看到视图但没有阴影
  • 你真的想要一个遮罩而不是一个子层吗?
  • @WilsonXJ 我将其更改为 addSubLayer 并且它可以工作,但现在我的形状是黑色的。而且我的颜色没有改变
  • 你可以放一张图片并描述你想要的效果

标签: ios swift uiview calayer cashapelayer


【解决方案1】:

感谢@WilsonXJ 我将掩码更改为 addSubLayer。

这是对我有用的答案:

    let p = UIBezierPath()
    p.moveToPoint(CGPointMake(20, 20))
    p.addLineToPoint(CGPointMake(100, 20))
    p.addLineToPoint(CGPointMake(100, 50))
    p.addLineToPoint(CGPointMake(110, 55))
    p.addLineToPoint(CGPointMake(100, 60))
    p.addLineToPoint(CGPointMake(100, 100))
    p.addLineToPoint(CGPointMake(20, 100))
    p.closePath()

    let s = CAShapeLayer()
    s.fillColor = UIColor.whiteColor().CGColor
    s.frame = layer.bounds
    s.path = p.CGPath

    layer.backgroundColor = UIColor.clearColor().CGColor
    layer.addSublayer(s)

    layer.masksToBounds = true
    layer.shadowColor = UIColor.yellowColor().CGColor
    layer.shadowOffset = CGSizeZero
    layer.shadowOpacity = 0.9
    layer.shadowPath = p.CGPath
    layer.shadowRadius = 10

【讨论】:

  • 子视图隐藏 ,,,,
【解决方案2】:

我不认为当前的答案是正确的,因为不再使用 layer.mask

如果您需要使用layer.mask 并在蒙版图层上放置阴影 - 显而易见的解决方案是在蒙版图层下方添加另一个图层,该图层与layer.mask 具有相同的形状并删除它的阴影

示例:

let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 500, height: 500)))
view.backgroundColor = .white
PlaygroundPage.current.liveView = view

let path: CGPath = ...

let maskedView = UIView(frame: path.boundingBox)
maskedView.center = view.center
maskedView.backgroundColor = .green
view.addSubview(maskedView)

let maskLayer = CAShapeLayer()
maskLayer.frame = maskedView.bounds
maskLayer.path = path
maskedView.layer.mask = maskLayer

let shadowLayer = CAShapeLayer()
shadowLayer.path = path
shadowLayer.frame = maskedView.frame

shadowLayer.shadowOpacity = 0.4
shadowLayer.shadowRadius = 2
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowOffset = CGSize(width: 4, height: 4)

maskedView.superview!.layer.insertSublayer(shadowLayer, below: maskedView.layer)

【讨论】:

    【解决方案3】:

    如果您不希望您的子视图被图层覆盖,请替换:

            layer.addSublayer(s)
    

    通过

            for subview in subviews {
                layer.insertSublayer(s, below: subview.layer)
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-19
      • 2012-06-07
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多