-
-
Save mihai-salari/529ec417cec0033e0271eb0df226135d to your computer and use it in GitHub Desktop.
Sine Wave with fill using UIBezierPath
This file contains 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() | |
// 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