gpt4 book ai didi

ios - 如何屏蔽和添加阴影到 UIView

转载 作者:IT王子 更新时间:2023-10-29 05:27:16 26 4
gpt4 key购买 nike

我正在制作一个自定义 View ,我想屏蔽它并为其添加阴影。

掩码:

    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

我尝试将它添加到 mask 层,但主视图被阴影遮住了。

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

有什么关于如何将这个黄色阴影添加到蒙版 View 的建议吗?

谢谢

最佳答案

感谢@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

关于ios - 如何屏蔽和添加阴影到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37567675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com