Skip to content

Instantly share code, notes, and snippets.

@tbaranes
Created February 5, 2016 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbaranes/cd9819512b80babb00bf to your computer and use it in GitHub Desktop.
Save tbaranes/cd9819512b80babb00bf to your computer and use it in GitHub Desktop.
UIBezierPath extension to make wawy lines
```swift
extension UIBezierPath {
class func bezierPathWithWavesInRect(rect: CGRect, width: CGFloat, offset: CGFloat = 0.0, waveUp: Bool = false) -> UIBezierPath {
let originY = waveUp ? rect.maxY : rect.minY
let halfWidth = width / 2.0
let halfHeight = rect.height / 2.0
let quarterWidth = width / 4.0
var up = waveUp
var startX = rect.minX - quarterWidth - (offset % width)
var endX = startX + halfWidth
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: startX, y: originY))
path.addLineToPoint(CGPoint(x: startX, y: rect.midY))
repeat {
path.addQuadCurveToPoint(
CGPoint(x: endX, y: rect.midY),
controlPoint: CGPoint(
x: startX + quarterWidth,
y: up ? rect.maxY + halfHeight : rect.minY - halfHeight)
)
startX = endX
endX += halfWidth
up = !up
} while startX < rect.maxX
path.addLineToPoint(CGPoint(x: path.currentPoint.x, y: originY))
return path
}
}
```
Usage:
```swift
class TestView: UIView {
override func drawRect(rect: CGRect) {
let wavesDown = UIBezierPath.bezierPathWithWavesInRect(self.bounds, width: 40.0, waveUp: true)
let wavesUp = UIBezierPath.bezierPathWithWavesInRect(self.bounds, width: 100.0, waveUp: true)
UIColor.blueColor().set()
wavesDown.stroke()
UIColor.yellowColor().set()
wavesUp.stroke()
}
}
let view = TestView(frame: CGRect(x: 0, y: 0, width: 800, height: 40))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment