-
-
Notifications
You must be signed in to change notification settings - Fork 535
Expand file tree
/
Copy pathMainView.swift
More file actions
157 lines (138 loc) · 5.16 KB
/
Copy pathMainView.swift
File metadata and controls
157 lines (138 loc) · 5.16 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2025 the FloatingPanel authors. All rights reserved. MIT license.
import FloatingPanel
import SwiftUI
import UIKit
import os.log
struct MainView: View {
enum CardContent: String, CaseIterable, Identifiable {
case list
case detail
var id: String { rawValue }
}
@State private var panelLayout: FloatingPanelLayout? = MyFloatingPanelLayout()
@State private var panelState: FloatingPanelState?
@State private var selectedContent: CardContent = .list
@State private var lastEvent: MyPanelCoordinator.Event?
var body: some View {
ZStack {
Color.orange
.ignoresSafeArea()
VStack(spacing: 32) {
Picker("type", selection: $selectedContent) {
ForEach(CardContent.allCases) {
type in
Text(type.rawValue).tag(type)
}
}
.pickerStyle(.segmented)
Text("Last event: \(lastEvent?.rawValue ?? "None")")
Button("Move to full") {
withAnimation(.interactiveSpring) {
panelState = .full
}
}
Button {
withAnimation(.interactiveSpring) {
if panelLayout is MyFloatingPanelLayout {
panelLayout = nil
} else {
panelLayout = MyFloatingPanelLayout()
}
}
} label: {
if panelLayout is MyFloatingPanelLayout {
Text("Switch to Default layout")
} else {
Text("Switch to My layout")
}
}
Spacer()
}
}
.floatingPanel(
coordinator: MyPanelCoordinator.self,
onEvent: onEvent
) { proxy in
switch selectedContent {
case .list:
ContentView(proxy: proxy)
case .detail:
HStack {
Spacer()
VStack {
Text("Detail content")
.padding(.top, 32)
Spacer()
}
Spacer()
}
.padding()
.background {
BackgroundView()
}
}
}
.floatingPanelSurfaceAppearance(.transparent())
.floatingPanelLayout(panelLayout)
.floatingPanelState($panelState)
.onChange(of: panelState) { newValue in
Logger().debug("Panel state changed: \(newValue ?? .hidden)")
}
}
func onEvent(_ event: MyPanelCoordinator.Event) {
lastEvent = event
}
}
// A custom coordinator object which handles panel context updates and setting up `FloatingPanelControllerDelegate` methods
class MyPanelCoordinator: FloatingPanelCoordinator {
enum Event: String {
case willBeginDragging
case didEndAttracting
}
let action: (Event) -> Void
let proxy: FloatingPanelProxy
required init(action: @escaping (MyPanelCoordinator.Event) -> Void) {
self.action = action
self.proxy = .init(controller: FloatingPanelController())
}
func setupFloatingPanel<Main, Content>(
mainHostingController: UIHostingController<Main>,
contentHostingController: UIHostingController<Content>
) where Main: View, Content: View {
// Set this as the delegate object
controller.delegate = self
// Set up the content
contentHostingController.view.backgroundColor = .clear
controller.set(contentViewController: contentHostingController)
// Show the panel
controller.addPanel(toParent: mainHostingController, animated: false)
}
func onUpdate<Representable>(
context: UIViewControllerRepresentableContext<Representable>
) where Representable: UIViewControllerRepresentable {}
}
extension MyPanelCoordinator: FloatingPanelControllerDelegate {
func floatingPanelWillBeginDragging(_ fpc: FloatingPanelController) {
action(.willBeginDragging)
}
func floatingPanelDidEndAttracting(_ fpc: FloatingPanelController) {
action(.didEndAttracting)
}
func floatingPanelDidChangeState(_ fpc: FloatingPanelController) {
// NOTE: This timing is difference from one of the change of the binding value
// to `floatingPanelState(_:)` modifier
}
}
// A custom layout object
class MyFloatingPanelLayout: FloatingPanelLayout {
let position: FloatingPanelPosition = .bottom
let initialState: FloatingPanelState = .tip
let anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] = [
.full: FloatingPanelLayoutAnchor(absoluteInset: 16.0, edge: .top, referenceGuide: .safeArea),
.half: FloatingPanelLayoutAnchor(fractionalInset: 0.4, edge: .bottom, referenceGuide: .safeArea),
.tip: FloatingPanelLayoutAnchor(absoluteInset: 44.0, edge: .bottom, referenceGuide: .safeArea),
]
}
#Preview("MainView") {
MainView()
}