I’m working on creating a "bridge" shape in SwiftUI by combining a rectangle and a circle. I believe using a method like .union might be the solution, but I’m having trouble figuring out how to implement it correctly. My goal is to merge a rectangle and a circle to create a shape that resembles a bridge. Specifically, I want the top side of the rectangle to have an inset curve. Additionally, I’d like to place an image in the background of this composite shape. Essentially, the final design should be a rectangle with a curved inset at the top and an image set behind it.
Some help woud be great! thanks
ZStack {
Rectangle()
.fill(.blue)
.frame(height:250)
Image("BGImage")
Circle()
.fill(.white)
.frame(width: 700)
.offset(y:-400)
}
Thats the goal:
2
Answers
Replace "BGImage" with the name of your image asset. Adjust the values of bridgeHeight and curveHeight in the BridgeShape struct to modify the appearance of the bridge as needed.
When you say you want to:
do you mean, the image should be behind the shape (in other words, the shape covers parts of the image), or do you want the shape to be filled with the image?
If you want the image to be behind the shape then a
Shape
is probably the best approach, because then the image will then show through the non-filled area(s) of the shape. There is another answer which shows how this is done.However, if you want the shape to be filled with the image then it sounds like we are just talking about taking a "bite" out of the image. This can be done with an overlay.
For the overlay approach, you might want the size of the bite to depend on the width of the image. A
GeometryReader
can be used to measure this.Like this: