読み込み中に使えそうなアニメーション


### 開発環境

+ XCode7.3
+ Swift2.2



定期的に作りたくなるアニメーション。
今すぐ使う予定はありません。

![20160702.gif](https://qiita-image-store.s3.amazonaws.com/0/40693/60df3aa5-6e13-242a-1ae5-988c655ae0ab.gif "20160702.gif")


```SubCALayer

import UIKit

class SubCALayer : CAShapeLayer {
    
    var onLight : Bool = false
    private var count : Int = 0
    
    override init() {
        super.init()
    }
    
    override init(layer: AnyObject) {
        super.init(layer: layer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func drawInContext(ctx: CGContext) {
        //背景
        let c = UIColor.whiteColor()
        CGContextSetFillColorWithColor(ctx, c.CGColor)
        CGContextAddRect(ctx, self.bounds)
        CGContextDrawPath(ctx, .Fill)
        
        var cc = UIColor.grayColor()
        
        let block : Int = 20
        let blank : Int = 10
        var cnt : Int = 0
        
        for i in 0 ..< 360 {
            if(i%(block+blank)==0){
                if(cnt == count && onLight){
                    cc = UIColor.orangeColor()
                } else {
                    cc = UIColor.grayColor()
                }
                cnt += 1
                let angle = CGFloat(i)
                CGContextMoveToPoint(ctx, CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
                let sa : CGFloat = angle * CGFloat(M_PI/180)
                let ea : CGFloat = ((angle + CGFloat(block-1)) * CGFloat(M_PI/180))
                
                CGContextAddArc(ctx, CGRectGetMidX(self.frame), CGRectGetMidY(self.frame), 100, sa, ea, 0)
                CGContextClosePath(ctx)
                CGContextSetFillColorWithColor(ctx, cc.CGColor)
                CGContextDrawPath(ctx, .Fill)
            }
        }
        
        //center circle
        CGContextAddArc(ctx, CGRectGetMidX(self.frame), CGRectGetMidY(self.frame), 80, 0, CGFloat(M_PI*2), 0)
        CGContextClosePath(ctx)
        CGContextSetFillColorWithColor(ctx, c.CGColor)
        CGContextDrawPath(ctx, .Fill)
    }
    
    func update(){
        if(count==11){
            count = 0
        } else {
            count += 1
        }
        self.setNeedsDisplay()
    }
    
    func start(){
        onLight = !onLight
    }
}

```


```ViewController
import UIKit

class ViewController: UIViewController {

    var subLayer : SubCALayer!
    var timer : NSTimer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        subLayer = SubCALayer()
        subLayer.frame = self.view.layer.frame
        self.view.layer.addSublayer(subLayer)
        subLayer.setNeedsDisplay()
        subLayer.start()
        
        timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(exec(_:)), userInfo: nil, repeats: true)
    }

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


    // timer execute
    @objc func exec(timer : NSTimer) {
        subLayer.update()
    }
}
```