I want the following shape to display rounded ends.
struct Wave: Shape {
var strength: Double
var frequency: Double
var phase: Double
var animatableData: AnimatablePair<Double, Double> {
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
}
}
I have tried to achieve this by displaying circles at the ends but I've not been able to match the size of the circles to the line-width of the shape when stroked. For example, if I draw the shape as follows:
Wave(strength: 50.0, frequency: 30, phase: 0)
.stroke(Color.white, lineWidth: 5.0)
The circles appear larger than the wave stroke.
I also tried with StrokeStyle
but it has no effect.
Wave(strength: 50.0, frequency: 30, phase: 0)
.stroke(Color.white, style: StrokeStyle(lineWidth: 5.0, lineCap: .round))
How can I achieve this?
I want the following shape to display rounded ends.
struct Wave: Shape {
var strength: Double
var frequency: Double
var phase: Double
var animatableData: AnimatablePair<Double, Double> {
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
}
}
I have tried to achieve this by displaying circles at the ends but I've not been able to match the size of the circles to the line-width of the shape when stroked. For example, if I draw the shape as follows:
Wave(strength: 50.0, frequency: 30, phase: 0)
.stroke(Color.white, lineWidth: 5.0)
The circles appear larger than the wave stroke.
I also tried with StrokeStyle
but it has no effect.
Wave(strength: 50.0, frequency: 30, phase: 0)
.stroke(Color.white, style: StrokeStyle(lineWidth: 5.0, lineCap: .round))
How can I achieve this?
Share Improve this question asked Nov 16, 2024 at 11:00 batmanbatman 2,4582 gold badges27 silver badges52 bronze badges 4 |2 Answers
Reset to default 0The .round
line cap style extends the line before making it round:
A line with a rounded end. Core Graphics draws the line to extend beyond the endpoint of the path. The line ends with a semicircular arc with a radius of 1/2 the line’s width, centered on the endpoint.
Therefore, if the end point of your line is right at the edge of the screen, the semicircle drawn will be outside of the bounds of the screen and be clipped.
You should add some padding to the Wave
,
.padding(.horizontal, lineWidth / 2)
The padding can be smaller than lineWidth / 2
depending on which way the line is facing. lineWidth / 2
is the "worst case", where the line is facing a horizontal direction.
You can achieve rounded shape line waves using the code below. In your code, I simply removed the left-end circle and the right-end circle. When using the Wave shape, you can apply a stroke with a specific style.
Custom wave code
struct Wave: Shape {
var strength: Double
var frequency: Double
var phase: Double
var animatableData: AnimatablePair<Double, Double> {
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
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
return path
}
}
you can use like this
struct CustomeRound: View {
var body: some View {
VStack{
Wave(strength: 50.0, frequency: 30, phase: 0)
.stroke(
Color.blue,
style: StrokeStyle(
lineWidth: 9,
lineCap: .round, // Rounded ends
lineJoin: .round // Rounded corners for sharp turns
)
)
Wave(strength: 50.0, frequency: 30, phase: 0)
.stroke(Color.black, style: StrokeStyle(lineWidth: 5.0, lineCap: .round))
}
.padding()
}
}
lineCap: .round
worked for me, on macOS 15.0. (image) What does it look like for you? – Sweeper Commented Nov 16, 2024 at 11:23lineCap: .round
extends the line bylineWidth / 2
then turns that into a semicircle, so it might not be visible if there is no padding. You should be able to see it if you add at leastlineWidth / 2
pts of padding. – Sweeper Commented Nov 16, 2024 at 11:27