最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - How to round ends of a custom shape in SwiftUI? - Stack Overflow

programmeradmin0浏览0评论

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 by lineWidth / 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 least lineWidth / 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
Add a comment  | 

2 Answers 2

Reset to default 0

The .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()
    }
}

发布评论

评论列表(0)

  1. 暂无评论