Swift 漸變色


示例:

//    設置漸變顏色方向
        //    gradientLayer.startPoint = CGPointMake(0, 0); // 左上
        //    gradientLayer.endPoint = CGPointMake(0, 1); // 左下
        //    gradientLayer.endPoint = CGPointMake(1, 0); // 右上
        //    gradientLayer.endPoint = CGPointMake(1, 1); // 右下

        if (indexPath.row==0) {
            
            //漸變設置
            let colorOne:UIColor = UIColor.init().UIColorFromRGB(color_vaule: "#1EB0FF")
            let colorTwo:UIColor = UIColor.init().UIColorFromRGB(color_vaule: "#035CD8")
       
            let colors = [colorOne.cgColor,colorTwo.cgColor];
            let gradient:CAGradientLayer = CAGradientLayer.init();
            //設置開始和結束位置(通過開始和結束位置來控制漸變的方向)
            gradient.startPoint = CGPoint.init(x: 0, y: 0.5)
            gradient.endPoint = CGPoint.init(x: 1, y: 0.5)
            gradient.colors = colors;
            gradient.frame = mycell.bounds;
            gradient.cornerRadius=20;
            mycell.contentView.layer.insertSublayer(gradient, at: 0)
        
        }
        else if (indexPath.row==1){
        
            //漸變設置
             let colorOne:UIColor = UIColor.init().UIColorFromRGB(color_vaule: "#FFA156")
             let colorTwo:UIColor = UIColor.init().UIColorFromRGB(color_vaule: "#FF722D")
        
             let colors = [colorOne.cgColor,colorTwo.cgColor];
             let gradient:CAGradientLayer = CAGradientLayer.init();
             //設置開始和結束位置(通過開始和結束位置來控制漸變的方向)
             gradient.startPoint = CGPoint.init(x: 0, y: 0.5)
             gradient.endPoint = CGPoint.init(x: 1, y: 0.5)
             gradient.colors = colors;
             gradient.frame = mycell.bounds;
            gradient.cornerRadius=20;
             mycell.contentView.layer.insertSublayer(gradient, at: 0)
            
            
        }
        else{
       
            let colorOne:UIColor = UIColor.init().UIColorFromRGB(color_vaule: "#BCEE4F")
             let colorTwo:UIColor = UIColor.init().UIColorFromRGB(color_vaule: "#63CB52")
        
             let colors = [colorOne.cgColor,colorTwo.cgColor];
             let gradient:CAGradientLayer = CAGradientLayer.init();
             //設置開始和結束位置(通過開始和結束位置來控制漸變的方向)
             gradient.startPoint = CGPoint.init(x: 0, y: 0.5)
             gradient.endPoint = CGPoint.init(x: 1, y: 0.5)
             gradient.colors = colors;
             gradient.frame = mycell.bounds;
            gradient.cornerRadius=20;
             mycell.contentView.layer.insertSublayer(gradient, at: 0)
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM