iOS ~CollectionView 渐变色背景

2019-02-14  本文已影响8人  派大星的博客
IMG_1574.JPG

代码实现

    private lazy var gradientLayer : CAGradientLayer = {
        let TColor : UIColor = UIColor(fromHexString: "FF8E8A")
        let BColor = UIColor.white
        let gradientColors: [CGColor] = [TColor.cgColor, BColor.cgColor]
        let gradientLayer: CAGradientLayer = CAGradientLayer()
        gradientLayer.colors = gradientColors
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 0, y: 1)
        gradientLayer.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
        return gradientLayer
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
        view.addSubview(collectionView)

        // **重点解析**
        gradientLayer.zPosition = -1
        collectionView.layer.insertSublayer(gradientLayer, at: 0)
    }

重点解析

zPosition

The layer’s position on the z axis. Animatable.

var zPosition: CGFloat { get set }

Discussion

The default value of this property is 0. Changing the value of this property changes the front-to-back ordering of layers onscreen. Higher values place this layer visually closer to the viewer than layers with lower values. This can affect the visibility of layers whose frame rectangles overlap.

译: 此属性的默认值为0。更改此属性的值将更改屏幕上层的前后顺序。较高的值比较低值的层在视觉上更接近查看器。这可能会影响框架矩形重叠的图层的可见性。

上一篇下一篇

猜你喜欢

热点阅读