円グラフの色をグラデーションしてみる


![20160403.gif](https://qiita-image-store.s3.amazonaws.com/0/40693/b7411f20-594e-85ef-40a1-39fb9f02c4e4.gif "20160403.gif")


例によってgif画像だと荒いけど、実際にはもう少し滑らかに色が変わっていきます。

環境
-

- Swift2.2
- Xcode7.3



```ViewController.swift
import UIKit

class customLayer : CAShapeLayer {
    
    var diameter : CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    override init() {
        super.init()
    }
    
    override init(layer: AnyObject) {
        if let layer = layer as? customLayer {
            diameter = layer.diameter
        }
        super.init(layer: layer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override class func  needsDisplayForKey(key: String) -> Bool {
        if (key == "diameter") {
            return true
        }
        return super.needsDisplayForKey(key)
    }

    override func drawInContext(ctx: CGContext) {

        let c = UIColor.whiteColor()
        CGContextSetFillColorWithColor(ctx, c.CGColor)
        CGContextAddRect(ctx, self.bounds)
        CGContextDrawPath(ctx, .Fill)
        
        
        for(var i : CGFloat = 180;i<diameter+180;i+=1){
            let cc = UIColor(hue: i/360, saturation: 1.0, brightness: 1.0, alpha: 1.0)
            CGContextMoveToPoint(ctx, 100, 100)
            let sa : CGFloat = 4.6 * (i+120) * CGFloat(M_PI/180)
            let r : CGFloat = i == 244 ? 4.6 : 4.61
            let ea : CGFloat = r * (i+121) * CGFloat(M_PI/180)
            
            CGContextAddArc(ctx, 100, 100, 100, sa, ea, 0)
            CGContextClosePath(ctx)
            CGContextSetFillColorWithColor(ctx, cc.CGColor)
            CGContextDrawPath(ctx, .Fill)
            
        }
        
        CGContextMoveToPoint(ctx, 100, 100)
        CGContextAddArc(ctx, 100, 100, 70, 0, CGFloat(360 * M_PI/180), 0)
        CGContextClosePath(ctx)
        CGContextSetFillColorWithColor(ctx, UIColor.whiteColor().CGColor)
        CGContextDrawPath(ctx, .Fill)
    }
}

class ViewController: UIViewController {
    
    var layer : customLayer!
    var label : UILabel!

     override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let sl = UISlider()
        sl.frame = CGRectMake(10, 250, 170, 20)
        sl.minimumValue = 0
        sl.maximumValue = 65
        sl.addTarget(self, action: #selector(ViewController.hoge(_:)) , forControlEvents: UIControlEvents.ValueChanged)
        view.addSubview(sl)
        
        layer = customLayer()
        layer.frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
        view.layer.addSublayer(layer)
        layer.setNeedsDisplay()
        
        label = UILabel()
        label.frame = CGRectMake(75, 85, 50, 25)
        label.backgroundColor = UIColor.clearColor()
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor(hue: 180/360, saturation: 1.0, brightness: 1.0, alpha: 1.0)
        label.text = "0%"
        view.addSubview(label)
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }
    
    func hoge(sender: AnyObject){
        let v = sender as! UISlider
        layer.diameter = CGFloat(v.value)
        
        let per : Int = Int(100/65*v.value)
        NSLog("%d", per)
        
        let i : CGFloat = CGFloat(v.value + 180)
        label.textColor = UIColor(hue: i/360, saturation: 1.0, brightness: 1.0, alpha: 1.0)
        label.text = String(per) +  "%"

    }
}
```

######補足
65=色の範囲(245-180)
4.6=円グラフ３００度÷色の範囲(245-180)
120=円グラフのスタート角度(300=180+120)