-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentView.swift
114 lines (106 loc) · 3.46 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import SwiftUI
struct Page: Identifiable {
let id = UUID()
var image: String?
var title: String?
var scale: CGFloat?
var view: AnyView
init<Content: View>(
image: String? = nil,
title: String? = nil,
scale: CGFloat? = nil,
@ViewBuilder view: () -> Content
) {
self.image = image
self.title = title
self.scale = scale
self.view = AnyView(view())
}
}
struct ContentView: View {
@State var selectedIndex = 12
let pages: [Page] = [
Page(image: "Padding", title: ".padding", scale: 1.2) {
PaddingView()
},
Page(image: "Background", title: ".background", scale: 1.5) {
BackgroundView()
},
Page(image: "Font", title: ".font", scale: 1.5) {
FontView()
},
Page(image: "Opacity", title: ".opacity", scale: 1.5) {
OpacityView()
},
Page(image: "ForegroundColor", title: ".foregroundColor", scale: 1.5) {
ForegroundColorView()
},
Page(image: "Corner", title: ".cornerRadius", scale: 1.5) {
CornerRadiusView()
},
Page(image: "Frame", title: ".frame", scale: 1.5) {
FrameView()
},
]
@State var selectedPage: Page?
var body: some View {
ScrollView {
Text("SwiftUI Modifiers")
.font(.system(size: 100, weight: .medium, design: .monospaced))
.foregroundColor(.purple)
Text("You can apply these to any View, including `Text`, `Image`, `VStack`, and `HStack`.")
.font(.system(size: 50))
LazyVGrid(
columns: [GridItem(.adaptive(minimum: 300))],
spacing: 16
) {
ForEach(pages) { page in
Button {
selectedPage = page
} label: {
VStack(spacing: 16) {
Text(page.title ?? "")
.font(.title)
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.padding()
.background(
Color.purple
)
page.view
.scaleEffect(page.scale ?? 1)
.frame(height: 200)
.padding()
}
.frame(maxWidth: .infinity)
.background(Color(uiColor: .systemBackground))
.cornerRadius(16)
}
.foregroundColor(.primary)
}
}
.padding()
}
.background(Color(uiColor: .secondarySystemBackground))
.fullScreenCover(item: $selectedPage) { page in
VStack(alignment: .trailing) {
Button {
selectedPage = nil
} label: {
Text("Done")
.padding()
}
if let image = page.image {
page.view.addContainer(image: image, title: page.title ?? "")
} else {
page.view
}
}
}
}
}
extension View {
func erased() -> AnyView {
AnyView(self)
}
}