如何将渐变应用于多个自定义 uiview?

Posted

技术标签:

【中文标题】如何将渐变应用于多个自定义 uiview?【英文标题】:How to apply gradients to mulitple custom uiviews? 【发布时间】:2018-04-02 10:19:26 【问题描述】:

我安排了 6 个自定义 uiview。我也尝试过使用各种技术应用渐变。

如何调用函数或如何为具有不同 cgcolor 代码的多个视图应用渐变。

        func onebg()
                    applyGradient(color_1: UIColor(red: 218.0/255.0, green: 52.0/255.0, blue: 204.0/255.0, alpha: 1.0).cgColor, color_2: UIColor(red: 149.0/255.0, green: 39.0/255.0, blue: 137.0/255.0, alpha: 1.0).cgColor, view: calendarView)
                

        func twobg()
                    applyGradient(color_1: UIColor(red: 104.0/255.0, green: 220.0/255.0, blue: 108.0/255.0, alpha: 1.0).cgColor, color_2: UIColor(red: 28.0/255.0, green: 170.0/255.0, blue: 87.0/255.0, alpha: 1.0).cgColor, view: clas-s-roomView)
                

        func threebg()
                    applyGradient(color_1: UIColor(red: 0.0/255.0, green: 144.0/255.0, blue: 214.0/255.0, alpha: 1.0).cgColor, color_2: UIColor(red: 34.0/255.0, green: 66.0/255.0, blue: 138.0/255.0, alpha: 1.0).cgColor, view: appointmentView)
                


        I have called this 6 methods like, onebg() twobg() from viewdidload(). But it is not applying and most importantly what i am getting is , the gradient color is applied only for last view. 

        func applyGradient(color_1 : CGColor , color_2: CGColor , view : UIView)  

                    gradientLayer.frame = view.bounds

                    let color1 = color_1

                    let color2 = color_2

                    gradientLayer.colors = [color1, color2]

                    gradientLayer.locations = [0.0, 0.75, 0.25, 1.0]

                    view.layer.insertSublayer(gradientLayer, at: 0)
        

这是我要求应用的方法,通过为渐变添加插入子层。请帮助。

【问题讨论】:

在viewDidLoad中添加代码 已经尝试过了,但是渐变只应用于最后一个视图,这也不是正确的方式。 我的意思是添加您的完整尝试 我尝试过的可能性: 1. 将 onebg(), twobg()... 放在视图中确实加载了。 2.将onebg()、twobg()放在一个单独的方法中,在viewdidload()中调用collection方法。 3. 直接从 viewdidload() 应用渐变。任何其他选择。 【参考方案1】:

Swift 4.0

您需要在一个文件中实现此代码并分配给视图,在检查器中配置您的渐变属性,如下图所示

你可以试试这个方法。

@IBDesignable class xGradientView: UIView 

    @IBInspectable
    var cornerRadius: CGFloat 
        get 
            return layer.cornerRadius
        
        set 
            layer.cornerRadius = newValue
        
    

    @IBInspectable
    var borderWidth: CGFloat 
        get 
            return layer.borderWidth
        
        set 
            layer.borderWidth = newValue
        
    

    @IBInspectable
    var borderColor: UIColor? 
        get 
            if let color = layer.borderColor 
                return UIColor(cgColor: color)
            
            return nil
        
        set 
            if let color = newValue 
                layer.borderColor = color.cgColor
             else 
                layer.borderColor = nil
            
        
    

    @IBInspectable
    var shadowRadius: CGFloat 
        get 
            return layer.shadowRadius
        
        set 
            layer.shadowRadius = newValue
        
    
    @IBInspectable
    var shadowOpacity: Float 
        get 
            return layer.shadowOpacity
        
        set 
            layer.shadowOpacity = newValue
        
    

    @IBInspectable
    var shadowOffset: CGSize 
        get 
            return layer.shadowOffset
        
        set 
            layer.shadowOffset = newValue
        
    

    @IBInspectable
    var shadowColor: UIColor? 
        get 
            if let color = layer.shadowColor 
                return UIColor(cgColor: color)
            
            return nil
        
        set 
            if let color = newValue 
                layer.shadowColor = color.cgColor
             else 
                layer.shadowColor = nil
            
        
    

//    MARK:- Gradient Color

    @IBInspectable var startColor:   UIColor = .clear  didSet  updateColors() 
    @IBInspectable var endColor:     UIColor = .clear  didSet  updateColors() 
    @IBInspectable var startLocation: Double =   0.0  didSet  updateLocations() 
    @IBInspectable var endLocation:   Double =   0.0  didSet  updateLocations() 
    @IBInspectable var horizontalMode:  Bool =  false  didSet  updatePoints() 
    @IBInspectable var diagonalMode:    Bool =  false  didSet  updatePoints() 
    var gradientSet = [[CGColor]]()
    var currentGradient: Int = 0
    override class var layerClass: AnyClass  return CAGradientLayer.self 
    var gradientLayer: CAGradientLayer  return layer as! CAGradientLayer 
    func updatePoints() 
        if horizontalMode 
            gradientLayer.startPoint = diagonalMode ? CGPoint(x: 1, y: 0) : CGPoint(x: 0, y: 0.5)
            gradientLayer.endPoint   = diagonalMode ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0.5)

         else 
            gradientLayer.startPoint = diagonalMode ? CGPoint(x: 0, y: 0) : CGPoint(x: 0.5, y: 0)
            gradientLayer.endPoint   = diagonalMode ? CGPoint(x: 1, y: 1) : CGPoint(x: 0.5, y: 1)
        
    
    func updateLocations() 
        gradientLayer.locations = [startLocation as NSNumber, endLocation as NSNumber]
    
    func updateColors() 
        gradientLayer.colors    = [startColor.cgColor, endColor.cgColor]
    
    override func layoutSubviews() 
        super.layoutSubviews()
        gradientLayer.drawsAsynchronously = true
        gradientSet.append([startColor.cgColor, endColor.cgColor])
        gradientSet.append([endColor.cgColor, startColor.cgColor])
        updatePoints()
        updateLocations()
        updateColors()
    

【讨论】:

快乐编码...@NiranjanB

以上是关于如何将渐变应用于多个自定义 uiview?的主要内容,如果未能解决你的问题,请参考以下文章

带有 CAShapeLayer 的 UIView 不显示 CAGradientLayer

如何创建自定义 UIView?

使用来自多个视图控制器的自定义 UIView

在自定义 UIView 类 Iphone 上应用自动布局约束。

如何应用自定义渐变:类型转换错误

Swift - 自定义颜色主题和 SFSafariViewController