-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicSwitch.swift
65 lines (58 loc) · 2.3 KB
/
DynamicSwitch.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
//
// DynamicSwitch.swift
// StubenFuchs
//
// Created by Simon Giesen on 04.02.25.
//
import SwiftUI
struct DynamicSwitch: View {
var options: [String]
@Binding var selectedIndex: Int
var body: some View {
ZStack {
// Hintergrund mit Blur und Schatten
RoundedRectangle(cornerRadius: 15)
.fill(Color(.systemGray5).opacity(0.5))
.blur(radius: 10)
.shadow(color: Color.black.opacity(0.2), radius: 10, x: 0, y: 5)
// Hervorhebungsindikator für das ausgewählte Segment
GeometryReader { geometry in
let segmentWidth = geometry.size.width / CGFloat(options.count)
RoundedRectangle(cornerRadius: 15)
.fill(Color.white.opacity(0.3))
.frame(width: segmentWidth, height: geometry.size.height)
.offset(x: CGFloat(selectedIndex) * segmentWidth)
.animation(.easeInOut, value: selectedIndex)
}
// Dynamische Anzeige der Optionen
HStack(spacing: 0) {
ForEach(options.indices, id: \.self) { index in
Text(options[index])
.fontWeight(.bold)
.foregroundColor(selectedIndex == index ? .black : .gray)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.contentShape(Rectangle()) // Damit der ganze Bereich anklickbar ist
.onTapGesture {
withAnimation(.easeInOut) {
selectedIndex = index
}
}
}
}
}
.frame(height: 40)
.background(BlurView(style: .systemUltraThinMaterial))
.clipShape(RoundedRectangle(cornerRadius: 15))
.overlay(
RoundedRectangle(cornerRadius: 15)
.stroke(Color.white.opacity(0.3), lineWidth: 1)
)
}
}
struct BlurView: UIViewRepresentable {
var style: UIBlurEffect.Style
func makeUIView(context: Context) -> UIVisualEffectView {
UIVisualEffectView(effect: UIBlurEffect(style: style))
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) { }
}