【问题标题】:How would I add the shadows to a UIView gradient attached to the mainview in a UICollectionView如何将阴影添加到 UICollectionView 中附加到主视图的 UIView 渐变
【发布时间】:2020-05-21 22:25:07
【问题描述】:

我正在尝试使用集合单元格中的渐变向我的 uiview 添加自定义阴影,但我似乎正在倒退...我将此类 EHGradientView 添加到我在 UICollectionView 的主视图中。并通过更改似乎适用于我的个人项目的课程。但事实证明,影子很难。

IBDesignable
class EHGradientView: UIView {


        override class var layerClass: AnyClass {
            return CAGradientLayer.self
        }

        private var gLayer: CAGradientLayer {
            return self.layer as! CAGradientLayer
        }

        override init(frame: CGRect) {
            super.init(frame: frame)
            commonInit()
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            commonInit()
        }

        func commonInit() {

            gLayer.type = .axial

            gLayer.colors = [
                UIColor(red: 1, green: 0.46, blue: 0.467, alpha: 1).cgColor,

                UIColor(red: 1, green: 0.36, blue: 0.369, alpha: 1).cgColor

            ]
            gLayer.locations = [0, 1]
            gLayer.startPoint = CGPoint(x: 0.25, y: 0.5)
            gLayer.endPoint = CGPoint(x: 0.75, y: 0.5)

            // exaggerated colors so it's obvious

            gLayer.cornerRadius = 28

            shadow()


        }

      func shadow() {
         var shadows = UIView()

        shadows.frame = frame

        shadows.clipsToBounds = false

        addSubview(shadows)


        let shadowPath0 = UIBezierPath(roundedRect: shadows.bounds, cornerRadius: 10)

        let layer0 = CALayer()

        layer0.shadowPath = shadowPath0.cgPath

        layer0.shadowColor = UIColor(red: 1, green: 0.36, blue: 0.369, alpha: 0.4).cgColor

        layer0.shadowOpacity = 1

        layer0.shadowRadius = 20

        layer0.shadowOffset = CGSize(width: 0, height: 5)

        layer0.bounds = shadows.bounds

        layer0.position = shadows.center

        shadows.layer.addSublayer(layer0)


        var shapes = UIView()

        shapes.frame = frame

        shapes.clipsToBounds = true

        addSubview(shapes)


        let layer1 = CALayer()

        layer1.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor

        layer1.bounds = shapes.bounds

        layer1.position = shapes.center

        shapes.layer.addSublayer(layer1)


        shapes.layer.cornerRadius = 10



    }


这是我一直试图结合到它的阴影代码,以便阴影出现在它的下方并最终看起来像这样。

相反,我得到的是:

【问题讨论】:

标签: ios swift uiview uicollectionview shadow


【解决方案1】:

以下代码可能对您有所帮助。

backgroundColor = .systemPink
layer.cornerRadius = 20
layer.masksToBounds = false
layer.shadowColor = UIColor.red.cgColor
layer.shadowOpacity = 0.3
layer.shadowRadius = 9.0
layer.shadowOffset = CGSize(width: 1, height: 5)

【讨论】:

  • 这是按照屏幕截图的预期吗?
  • 我不知道把这段代码放在哪里。这在我的代码中适用于何处?特别是因为我有两层。
  • 创建一个包含上述代码的函数并在 override init(frame: CGRect) 方法中调用该函数。
猜你喜欢
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 2015-03-12
  • 2011-11-11
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多