Skip to content

Commit 41849f9

Browse files
committed
Add implementation of the custom row shape
1 parent da41aa0 commit 41849f9

File tree

4 files changed

+88
-15
lines changed

4 files changed

+88
-15
lines changed

Image.png

44.4 KB
Loading

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Custom List Row Shape
2+
3+
This project has a curved shape that's used for the background of a List's row. The shape draws outside of its bounds so that it curves upwards into another row's space.
4+
5+
![Image](Image.png)

Shared/ContentView.swift

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,79 @@
1-
//
2-
// ContentView.swift
3-
// Shared
4-
//
5-
// Created by Daniel Tull on 27.06.2020.
6-
//
71

82
import SwiftUI
93

4+
struct Item: Identifiable {
5+
var id: String { name }
6+
let name: String
7+
let color: Color
8+
}
9+
1010
struct ContentView: View {
11+
12+
let items: [Item]
13+
1114
var body: some View {
12-
Text("Hello, world!").padding()
15+
ScrollView {
16+
ForEach(items) { item in
17+
Text(item.name)
18+
.padding(.horizontal)
19+
.padding(.vertical, 40)
20+
.frame(maxWidth: .infinity)
21+
.background(CurvedShape(radius: 30).fill(item.color))
22+
}
23+
}
24+
.background(Color.gray)
25+
.frame(maxWidth: .infinity, maxHeight: .infinity)
26+
}
27+
}
28+
29+
struct CurvedShape: Shape {
30+
31+
let radius: CGFloat
32+
33+
func path(in rect: CGRect) -> Path {
34+
35+
var path = Path()
36+
path.move(to: CGPoint(x: rect.minX, y: rect.minY - radius))
37+
38+
path.addArc(center: CGPoint(x: rect.minX + radius, y: rect.minY - radius),
39+
radius: radius,
40+
startAngle: Angle(degrees: 180),
41+
endAngle: Angle(degrees: 90),
42+
clockwise: true)
43+
44+
path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
45+
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
46+
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
47+
48+
path.addArc(center: CGPoint(x: rect.minX + radius, y: rect.maxY - radius),
49+
radius: radius,
50+
startAngle: Angle(degrees: 90),
51+
endAngle: Angle(degrees: 180),
52+
clockwise: false)
53+
54+
path.addLine(to: CGPoint(x: rect.minX, y: rect.minY))
55+
56+
return path
1357
}
1458
}
1559

1660
struct ContentView_Previews: PreviewProvider {
1761
static var previews: some View {
18-
ContentView()
62+
ContentView(items: [
63+
Item(name: "Red", color: .red),
64+
Item(name: "Yellow", color: .yellow),
65+
Item(name: "Blue", color: .blue),
66+
Item(name: "Orange", color: .orange),
67+
Item(name: "Pink", color: .pink),
68+
Item(name: "Purple", color: .purple),
69+
Item(name: "Green", color: .green),
70+
Item(name: "Red", color: .red),
71+
Item(name: "Yellow", color: .yellow),
72+
Item(name: "Blue", color: .blue),
73+
Item(name: "Orange", color: .orange),
74+
Item(name: "Pink", color: .pink),
75+
Item(name: "Purple", color: .purple),
76+
Item(name: "Green", color: .green),
77+
])
1978
}
2079
}

Shared/Custom_List_Row_ShapeApp.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
//
2-
// Custom_List_Row_ShapeApp.swift
3-
// Shared
4-
//
5-
// Created by Daniel Tull on 27.06.2020.
6-
//
71

82
import SwiftUI
93

104
@main
115
struct Custom_List_Row_ShapeApp: App {
126
var body: some Scene {
137
WindowGroup {
14-
ContentView()
8+
ContentView(items: [
9+
Item(name: "Red", color: .red),
10+
Item(name: "Yellow", color: .yellow),
11+
Item(name: "Blue", color: .blue),
12+
Item(name: "Orange", color: .orange),
13+
Item(name: "Pink", color: .pink),
14+
Item(name: "Purple", color: .purple),
15+
Item(name: "Green", color: .green),
16+
Item(name: "Red", color: .red),
17+
Item(name: "Yellow", color: .yellow),
18+
Item(name: "Blue", color: .blue),
19+
Item(name: "Orange", color: .orange),
20+
Item(name: "Pink", color: .pink),
21+
Item(name: "Purple", color: .purple),
22+
Item(name: "Green", color: .green),
23+
])
1524
}
1625
}
1726
}

0 commit comments

Comments
 (0)