美文网首页
【Swift】CAGradientLayer 实现渐变色效果

【Swift】CAGradientLayer 实现渐变色效果

作者: BeethOven | 来源:发表于2020-03-20 12:03 被阅读0次

    在项目中的渐变色控件我继承自我的RLGrandientLayerView,实现
    Demo如图


    渐变色.gif

    源码:

    class RLGrandientLayerView: UIView {
        override class var layerClass: AnyClass {
            return CAGradientLayer.self
        }
        
        var gradientColors: [UIColor]?
        
        var locations: [NSNumber]?
        
        var startPoint: CGPoint = .zero
        
        var endPoint: CGPoint = CGPoint.init(x: 1, y: 1)
        
        required convenience init(grandientColors: [UIColor]) {
            self.init(frame: .zero)
            self.gradientColors = grandientColors
            locations = [0.0, 1.0]
        }
        
        override func layoutSubviews() {
            super.layoutSubviews()
            if let layer = layer as? CAGradientLayer {
                layer.colors = gradientColors?.map({ $0.cgColor })
                layer.locations = locations
                layer.startPoint = startPoint
                layer.endPoint = endPoint
            }
        }
        
    }
    
    

    相关文章

      网友评论

          本文标题:【Swift】CAGradientLayer 实现渐变色效果

          本文链接:https://www.haomeiwen.com/subject/wobsyhtx.html