-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCustomTabbarView.swift
140 lines (123 loc) · 3.61 KB
/
CustomTabbarView.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
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
//
// ContentView.swift
// CustomTabbar
//
// Created by Ganesh on 17/05/23.
//
import SwiftUI
struct CustomTabbarView: View {
init(){
UITabBar.appearance().isHidden = true
}
@State private var currentTab:Tab = .position
var body: some View {
VStack(spacing: 0) {
TabView(selection: $currentTab){
Text(currentTab.rawValue)
.applyBG()
.tag(currentTab.rawValue)
}
CustomTabbar(currentTab: $currentTab)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
CustomTabbarView()
}
}
struct CustomTabbar: View {
@Binding var currentTab:Tab
@State var yOffset:CGFloat = 0
var body: some View {
GeometryReader { proxy in
let width = proxy.size.width
HStack(spacing: 0) {
ForEach(Tab.allCases,id: \.self){ tab in
Button {
withAnimation(.easeOut(duration: 0.2)){
currentTab = tab
yOffset = -60
}
withAnimation(.easeOut(duration: 0.1).delay(0.07)){
yOffset = 0
}
} label: {
Image(systemName: getImage(rawValue: tab.rawValue))
.renderingMode(.template)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 25,height: 25)
.frame(maxWidth: .infinity)
.foregroundColor(currentTab == tab ? .green : .gray)
.scaleEffect(currentTab == tab ? 1.5 : 1)
}
}
}
.frame(maxWidth: .infinity)
.background(alignment:.leading){
Circle()
.fill(.white)
.frame(width: 50,height: 50)
.offset(x:10,y:yOffset)
.offset(x:indicatorOffset(width: width))
.shadow(color: .green, radius: 10)
}
}
.frame(height: 30)
.padding(.bottom,10)
.padding([.horizontal,.top])
}
func indicatorOffset(width:CGFloat) -> CGFloat{
let index = CGFloat(getIndex())
if index == 0 {
return 0
}
let buttonWidth = width / CGFloat(Tab.allCases.count)
return index * buttonWidth
}
func getIndex() -> Int{
switch currentTab {
case .home:
return 0
case .lable:
return 1
case .position:
return 2
case .found:
return 3
case .my:
return 4
}
}
func getImage(rawValue:String) -> String{
switch rawValue {
case "Home":
return "house.fill"
case "Label":
return "list.clipboard.fill"
case "Position":
return "location.circle.fill"
case "Found":
return "safari.fill"
case "My":
return "person.crop.circle"
default:
return ""
}
}
}
extension View{
func applyBG() -> some View{
self
.frame(maxWidth: .infinity,maxHeight: .infinity)
.ignoresSafeArea()
}
}
enum Tab: String,CaseIterable{
case home = "Home"
case lable = "Label"
case position = "Position"
case found = "Found"
case my = "My"
}