Created
March 13, 2015 17:44
-
-
Save quesera2/38aa605f99a9666a925d to your computer and use it in GitHub Desktop.
アニメーションするグラフ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class AnimatableGraph : CALayer { | |
var degree: Double = 0.0 { | |
didSet{ | |
if(degree < 0){ | |
self.degree = 0 | |
}else if(degree > 360){ | |
self.degree = 360.0 | |
} | |
} | |
} | |
override init!() { | |
super.init() | |
self.needsDisplayOnBoundsChange = true | |
self.contentsScale = UIScreen.mainScreen().scale | |
} | |
// アニメーション時にはこのイニシャライザを実装する必要がある | |
override init!(layer: AnyObject!) { | |
if let layer = layer as? AnimatableGraph { | |
degree = layer.degree | |
} | |
super.init(layer: layer) | |
needsDisplayOnBoundsChange = true | |
contentsScale = UIScreen.mainScreen().scale | |
} | |
// requiredなので | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
override class func needsDisplayForKey(key: String!) -> Bool { | |
if(key == "degree"){ | |
return true | |
} | |
return super.needsDisplayForKey(key) | |
} | |
override func drawInContext(ctx: CGContext!) { | |
CGContextSaveGState(ctx) | |
var x = self.bounds.origin.x | |
x += self.bounds.size.width/2 | |
var y = self.bounds.origin.y | |
y += self.bounds.size.height/2 | |
CGContextSetLineWidth(ctx, 0) | |
var path = CGPathCreateMutable() | |
CGPathAddArc(path, nil, x, y, CGFloat(x-10), 0, CGFloat(M_PI * 2), false) | |
CGContextAddPath(ctx, path); | |
CGContextSetRGBFillColor(ctx, CGFloat(0.6), CGFloat(0.6), CGFloat(0.6), CGFloat(1.0)); | |
CGContextDrawPath(ctx, kCGPathFillStroke); | |
// 3時の方向が0度なので補正 | |
var aDegree = degree - 180.0; | |
var graph = CGPathCreateMutable() | |
CGPathAddArc(graph, nil, x, y, CGFloat(x-10), CGFloat((M_PI * -1.0)), CGFloat((M_PI * 2.0) * (aDegree / 360.0)), false) | |
CGPathAddLineToPoint(graph, nil, x, y) | |
CGContextAddPath(ctx, graph); | |
CGContextSetRGBFillColor(ctx, CGFloat(232.0/255.0), CGFloat(38.0/255.0), CGFloat(107.0/255.0), CGFloat(1.0)) | |
CGContextDrawPath(ctx, kCGPathFillStroke); | |
CGContextRestoreGState(ctx); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
var graph = AnimatableGraph() | |
graph.frame = CGRect(x: 50, y: 50, width: 200, height: 200) | |
self.view.layer.addSublayer(graph) | |
var animation = CABasicAnimation(keyPath: "degree") | |
animation.duration = 10 | |
animation.repeatCount = 1 | |
animation.fromValue = 0.0 | |
animation.toValue = 360.0 | |
animation.removedOnCompletion = false | |
animation.fillMode = kCAFillModeForwards | |
graph.addAnimation(animation, forKey: "key") | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment