Created
February 23, 2016 19:27
-
-
Save soggybag/436a9a514ca497b8ef2b to your computer and use it in GitHub Desktop.
Animate drawing sine wave. CABasicAnimation and UIBezierPath
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
// Draw a sine curve with a fill | |
let centerY = frame.height / 2 // find the vertical center | |
let steps = 200 // Divide the curve into steps | |
let stepX = frame.width / CGFloat(steps) // find the horizontal step distance | |
// Make a path | |
let path = UIBezierPath() | |
path.moveToPoint(CGPoint(x: 0, y: centerY)) | |
// Loop and draw steps in straingt line segments | |
for i in 0...steps { | |
let x = CGFloat(i) * stepX | |
let y = (sin(Double(i) * 0.1) * 40) + Double(centerY) | |
path.addLineToPoint(CGPoint(x: x, y: CGFloat(y))) | |
} | |
pathLayer.path = path.CGPath | |
pathLayer.lineWidth = 3 | |
pathLayer.fillColor = UIColor.clearColor().CGColor | |
pathLayer.strokeColor = UIColor.redColor().CGColor | |
pathLayer.strokeStart = 0 | |
pathLayer.strokeEnd = 0 // << | |
let animation = CABasicAnimation(keyPath: "strokeEnd") | |
animation.fromValue = 0 | |
animation.toValue = 1 | |
animation.duration = 3 | |
pathLayer.addAnimation(animation, forKey: "strokeEnd") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment