ios - 如何将阴影添加到附加到 UICollectionView 中主视图的 UIView 渐变中

标签 ios swift uiview uicollectionview shadow

我正在尝试使用集合单元格中的渐变向我的 uiview 添加自定义阴影,但我似乎在向后做事...我将此类 EHGradientView 添加到 UICollectionView 中的主视图中。通过更改似乎对我的个人项目有效的类(class)。但事实证明,影子很难实现。

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



    }


这是我一直尝试组合的阴影代码,以便阴影出现在它的下面并最终看起来像这样。 enter image description here

相反,我得到的是:

enter image description here

最佳答案

下面的代码可能对您有帮助。

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)

modified properties values and got this result. May this help you.

关于ios - 如何将阴影添加到附加到 UICollectionView 中主视图的 UIView 渐变中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60085392/

相关文章:

iphone - iOS SplitView/Universal 帮助需要 xcode 4.2

ios - 检查 git diff 后,如何忽略一些我不想添加到此提交的文件中的更改?

swift - 如何检测第一个应用程序启动并显示说明屏幕?

ios - 如何在容器中使用 Swift 特定的容器?

ios - Swift-无论哪个ViewController启动,在应用启动时都显示showingSplashView

ios - 在 Swift 中声明采用符合 “can be multiplied” 的泛型类型的函数

ios - Xcode 显示加载 View ,直到加载第二个 View (在 segue show 上)

ios - 如何从消息过滤器扩展中获取数据?

iphone - 将 myClass 对象分配给 UIView 对象并使用 myClass 选择器(变量)

ios - 如何在SupplementaryView 上用tabbar 布局UICollectionView,并切换不同的内容?