Hi Friends,
In this tutorial I was explained about how to create a custom Shapes in SwiftUI. In SwiftUI some Shapes was provided by Apple. Like Circle, Rectangle etc.
If you need to create a new custom Shapes to show on the screen. How?
Here I have a solution for this question.
Shape is the one of the Protocol in SwiftUI. So, We have to create Struct with conform of Shape Protocol.
Here I create the Shape and named like WaveShape.
struct WaveShape: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.minX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY - 50))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
path.move(to: CGPoint(x: rect.minX, y: rect.maxY))
path.addCurve(to: CGPoint(x: rect.maxX, y: rect.maxY - 50),
control1: CGPoint(x: rect.minX, y: rect.midY),
control2: CGPoint(x: rect.midX, y: rect.maxY))
return path
}
}
Now. Shape creation was completed. But How to test this? Here I create on View and use WaveShape in it. Just look How is that.
struct SceneTitleView: View {
var title: String
var height: CGFloat = 140
var body: some View {
VStack {
ZStack {
ZStack {
WaveShape()
.fill(Color.blue)
.opacity(0.4)
.frame(height: height)
.shadow(color: .black, radius: 2, x: 0.0, y: 0.0)
WaveShape()
.fill(Color.blue)
.frame(height: height)
.offset(x: 0, y: -20.0)
.shadow(color: .black, radius: 4, x: 0.0, y: 0.0)
}
HStack {
Text(title)
.font(.largeTitle)
.fontWeight(.medium)
.foregroundColor(.white)
.padding()
.offset(x: 0, y: -20.0)
Spacer()
}
}
}
}
}
struct SceneTitleView_Previews: PreviewProvider {
static var previews: some View {
SceneTitleView(title: "Screen Title")
.previewLayout(.sizeThatFits)
}
}
How it is? Its Awesome know.
Thanks for Visiting Subscribe my blog. Also, Click below to visit my YouTube channel.