2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

開発環境

  • XCode7.3
  • Swift2.2

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

20160702.gif


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
    }
}

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()
    }
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?