admin管理员组文章数量:1026989
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()
}
}
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-
You don't need to hand draw the circles. Just using
lineCap: .round
worked for me, on macOS 15.0. (image) What does it look like for you? – Sweeper Commented Nov 16, 2024 at 11:23 -
One possibility I can think of is that you are drawing the shape at the very edge of the screen.
lineCap: .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 - You're right in that the shape is clipped by the edges of the screen. I will check with padding. – batman Commented Nov 16, 2024 at 11:40
- @Sweeper, you were right. Adding the padding fixed the issue. Would you like to add your suggestion as an answer? – batman Commented Nov 16, 2024 at 12:47
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()
}
}
本文标签: iosHow to round ends of a custom shape in SwiftUIStack Overflow
版权声明:本文标题:ios - How to round ends of a custom shape in SwiftUI? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659847a2161822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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