【问题标题】:How to add shadow to a transparent UIView?如何为透明的 UIView 添加阴影?
【发布时间】:2012-06-07 08:05:39
【问题描述】:

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

【问题讨论】:

    标签: ios quartz-graphics transparent alpha shadow


    【解决方案1】:

    尝试使用您的视图的这些属性。它可能最终会发生一些事情

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

    如果 CGPathRelease(rect) 最后很抱歉,它会泄漏。

    【讨论】:

    • 当视图透明时这不起作用 - alpha 0
    【解决方案2】:

    在搜索并没有找到这个问题的答案后,我在 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
        }
    }
    
    

    用法示例:

    myView.makeClearViewWithShadow(cornderRadius: 20, shadowColor: UIColor.black.cgColor, shadowOpacity: 0.5, shadowRadius: 30)
    

    【讨论】:

      【解决方案3】:

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

      没有本地方法可以满足您的要求,

      【讨论】:

        【解决方案4】:

        我使用 UIImageView 来提供阴影。我创建了一个 1 像素宽、12 像素高的图像。图像是黑色的,但 alpha 是从顶部的 0.5 到底部的 0.0 的渐变,例如。 RGBA 1.0,1.0,1.0,0.5 -> 1.0,1.0,1.0,0.0 - 然后我将 UIImageView 放在需要阴影的视图下方,并将其水平拉伸到视图的宽度上。 当我需要在下面的滚动视图滚动时通过更改 UIImageView.alpha 值来更改阴影的强度时,我也使用了它

        【讨论】:

          【解决方案5】:

          我的解决方案是继承 UIView。即使调整视图大小,它也能正常工作。

          import UIKit
          
          class ShadowOutsideView: UIView {
            let color: CGColor
            let opacity: Float
            let radius: CGFloat
            let offset: CGSize
            let shadowView = UIView()
            
            init(
              color: CGColor,
              opacity: Float,
              radius: CGFloat,
              offset: CGSize
            ) {
              self.color = color
              self.opacity = opacity
              self.radius = radius
              self.offset = offset
              super.init(frame: CGRect.zero)
              shadowView.frame = bounds
              shadowView.backgroundColor = .black
              shadowView.layer.shadowColor = color
              shadowView.layer.shadowOpacity = opacity
              shadowView.layer.shadowRadius = radius
              shadowView.layer.shadowOffset = offset
              shadowView.layer.masksToBounds = false
              self.addSubview(shadowView)
              
            }
            
            override func layoutSubviews() {
              shadowView.frame = bounds
              let inset = -(max(abs(offset.width), abs(offset.height))+radius) - 10 
              // - 10, had to put 10px extra otherwise it would cut shadow
              let maskFrame = bounds.inset(by: UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset))
              let p = CGMutablePath()
              p.addRect(maskFrame)
              p.addRect(bounds)
              let s = CAShapeLayer()
              s.path = p
              s.fillRule = .evenOdd
              shadowView.layer.mask = s
            }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-02-04
            • 1970-01-01
            • 2018-07-19
            • 2016-09-30
            • 1970-01-01
            • 2015-10-23
            • 1970-01-01
            相关资源
            最近更新 更多