Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Created February 13, 2021 12:41
Show Gist options
  • Save prafullakumar/83603f507f08f7b5c727d94549a0fd60 to your computer and use it in GitHub Desktop.
Save prafullakumar/83603f507f08f7b5c727d94549a0fd60 to your computer and use it in GitHub Desktop.
struct Wave: Shape {
var offset: Angle
var percent: Double
var animatableData: Double {
get { offset.degrees }
set { offset = Angle(degrees: newValue) }
}
func path(in rect: CGRect) -> Path {
var p = Path()
let waveHeight = 0.015 * rect.height
let yOffset = CGFloat(1 - percent) * (rect.height - 4 * waveHeight) + 2 * waveHeight
let startAngle = offset
let endAngle = offset + Angle(degrees: 360)
p.move(to: CGPoint(x: 0, y: yOffset + waveHeight * CGFloat(sin(offset.radians))))
for angle in stride(from: startAngle.degrees, through: endAngle.degrees, by: 5) {
let x = CGFloat((angle - startAngle.degrees) / 360) * rect.width
p.addLine(to: CGPoint(x: x, y: yOffset + waveHeight * CGFloat(sin(Angle(degrees: angle).radians))))
}
p.addLine(to: CGPoint(x: rect.width, y: rect.height))
p.addLine(to: CGPoint(x: 0, y: rect.height))
p.closeSubpath()
return p
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment