Skip to content

Instantly share code, notes, and snippets.

@soggybag
Created February 23, 2016 19:04
Show Gist options
  • Save soggybag/722640423cbc412bfc05 to your computer and use it in GitHub Desktop.
Save soggybag/722640423cbc412bfc05 to your computer and use it in GitHub Desktop.
Sine Wave with fill using UIBezierPath
// 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()
// Start in the lower left corner
path.moveToPoint(CGPoint(x: 0, y: frame.height))
// Draw a line up to the vertical center
path.addLineToPoint(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)))
}
// Draw down to the lower right
path.addLineToPoint(CGPoint(x: frame.width, y: frame.height))
// Close the path
path.closePath()
// Render the path
let fillColor = UIColor.redColor()
fillColor.setFill()
path.fill()
// let strokeColor = UIColor.redColor()
// strokeColor.setStroke()
// path.lineWidth = 3
// path.stroke()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment