Код: Выделить всё
struct Wave: Shape {
var strength: Double
var frequency: Double
var phase: Double
var animatableData: AnimatablePair {
get { AnimatablePair(phase, strength) }
set {
self.phase = newValue.first
self.strength = newValue.second
}
}
func path(in rect: CGRect) -> Path {
var path = Path()
let width = Double(rect.width)
let height = Double(rect.height)
let midHeight = height / 2
let wavelength = width / frequency
let strokeWidth = 5.0
let circleRadius = strokeWidth / 2
let firstX = 0.0
let firstRelativeX = firstX / wavelength
let firstSine = sin(firstRelativeX + phase)
let firstY = strength * firstSine + midHeight
// Left-end circle
path.addEllipse(in: CGRect(
x: firstX - circleRadius,
y: firstY - circleRadius,
width: circleRadius * 2,
height: circleRadius * 2
))
path.move(to: CGPoint(x: firstX, y: firstY))
for x in stride(from: 0.0, through: width, by: 1) {
let relativeX = x / wavelength
let sine = sin(relativeX + phase)
let y = strength * sine + midHeight
path.addLine(to: CGPoint(x: x, y: y))
}
let lastX = width
let lastRelativeX = lastX / wavelength
let lastSine = sin(lastRelativeX + phase)
let lastY = strength * lastSine + midHeight
// Right-end circle
path.addEllipse(in: CGRect(
x: lastX - circleRadius,
y: lastY - circleRadius,
width: circleRadius * 2,
height: circleRadius * 2
))
return path
}
}
Код: Выделить всё
Wave(strength: 50.0, frequency: 30, phase: 0)
.stroke(Color.white, lineWidth: 5.0)
Я также пробовал использовать StrokeStyle, но это не дало эффекта.
Код: Выделить всё
Wave(strength: 50.0, frequency: 30, phase: 0)
.stroke(Color.white, style: StrokeStyle(lineWidth: 5.0, lineCap: .round))
Подробнее здесь: https://stackoverflow.com/questions/791 ... in-swiftui