|
1 |
| -// |
2 |
| -// ContentView.swift |
3 |
| -// Shared |
4 |
| -// |
5 |
| -// Created by Daniel Tull on 27.06.2020. |
6 |
| -// |
7 | 1 |
|
8 | 2 | import SwiftUI
|
9 | 3 |
|
| 4 | +struct Item: Identifiable { |
| 5 | + var id: String { name } |
| 6 | + let name: String |
| 7 | + let color: Color |
| 8 | +} |
| 9 | + |
10 | 10 | struct ContentView: View {
|
| 11 | + |
| 12 | + let items: [Item] |
| 13 | + |
11 | 14 | 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 |
13 | 57 | }
|
14 | 58 | }
|
15 | 59 |
|
16 | 60 | struct ContentView_Previews: PreviewProvider {
|
17 | 61 | 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 | + ]) |
19 | 78 | }
|
20 | 79 | }
|
0 commit comments