如何在透明的UIView中添加阴影?

Ján*_*nos 6 alpha transparent shadow quartz-graphics ios

我该如何重现这个阴影?我的问题,如果我使用高alpha颜色或clearColor,没有阴影,如果我使用低alpha颜色,我看不到板下的阴影.随着音量水平的变化(彩色部分)阴影也在移动,所以它不是纯粹的Photoshop.

在此输入图像描述

Nic*_*ini 9

尝试一些使用你的观点的东西.它可能最终得到一些东西

    view.layer.shadowColor = [UIColor colorWithWhite:.5 alpha:1].CGColor;
    view.layer.shadowRadius = 4.0f;
    view.layer.shadowPath = CGPathCreateWithRect(CGRectMake(0, 0, 50, 50), NULL);
    view.layer.shadowOpacity = 1.0f;
    view.layer.shadowOffset = CGSizeMake(1, 1);
Run Code Online (Sandbox Code Playgroud)

它应该是CGPathRelease(rect)最后泄漏对不起.


Ros*_*ner 6

在搜索并没有找到这个问题的答案后,我在 Swift 5 中编写了以下扩展:

extension UIView {
    func makeClearViewWithShadow(
        cornderRadius: CGFloat,
        shadowColor: CGColor,
        shadowOpacity: Float,
        shadowRadius: CGFloat) {

        self.frame = self.frame.insetBy(dx: -shadowRadius * 2,
                                        dy: -shadowRadius * 2)
        self.backgroundColor = .clear
        let shadowView = UIView(frame: CGRect(
            x: shadowRadius * 2,
            y: shadowRadius * 2,
            width: self.frame.width - shadowRadius * 4,
            height: self.frame.height - shadowRadius * 4))
        shadowView.backgroundColor = .black
        shadowView.layer.cornerRadius = cornderRadius
        shadowView.layer.borderWidth = 1.0
        shadowView.layer.borderColor = UIColor.clear.cgColor

        shadowView.layer.shadowColor = shadowColor
        shadowView.layer.shadowOpacity = shadowOpacity
        shadowView.layer.shadowRadius = shadowRadius
        shadowView.layer.masksToBounds = false
        self.addSubview(shadowView)

        let p:CGMutablePath = CGMutablePath()
        p.addRect(self.bounds)
        p.addPath(UIBezierPath(roundedRect: shadowView.frame, cornerRadius: shadowView.layer.cornerRadius).cgPath)

        let s = CAShapeLayer()
        s.path = p
        s.fillRule = CAShapeLayerFillRule.evenOdd

       self.layer.mask = s
    }
}

Run Code Online (Sandbox Code Playgroud)

用法示例:

myView.makeClearViewWithShadow(cornderRadius: 20, shadowColor: UIColor.black.cgColor, shadowOpacity: 0.5, shadowRadius: 30)
Run Code Online (Sandbox Code Playgroud)


Oma*_*ith 0

您将有两个图像,一个是彩色图像,另一个是它的阴影,阴影会随着彩色部分的扩展而扩展并增加宽度

没有本地方法可以完成您所要求的事情,