Created
February 23, 2016 18:56
-
-
Save soggybag/e8db3206a887d6d20710 to your computer and use it in GitHub Desktop.
Short snippet for drawing a sine curve with 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 | |
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() | |
// Move the starting point to the left center | |
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 = CGFloat(i) % 2 * 10 + centerY | |
let y2 = (sin(Double(i) * 0.1) * 40) + Double(centerY) | |
path.addLineToPoint(CGPoint(x: x, y: CGFloat(y2))) | |
} | |
/* | |
let path = UIBezierPath(rect: CGRect(x: 10, y: 10, width: 40, height: 40)) | |
*/ | |
// Render the path | |
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