teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2021/09/07 08:31

投稿

shiokara
shiokara

スコア95

answer CHANGED
@@ -11,4 +11,123 @@
11
11
  ```Swift
12
12
  ForEach(0..<3) { (index) in
13
13
  ```
14
- タブを生成しているこの場所の`3`の部分を`Defs.tabItems.count`に書き換えれば増やせると思います。
14
+ タブを生成しているこの場所の`3`の部分を`Defs.tabItems.count`に書き換えれば増やせると思います。
15
+
16
+ 追記
17
+ レイアウトを直した全体コード
18
+ ```Swift
19
+ import SwiftUI
20
+ import MapKit
21
+
22
+ struct TopFrameView: Shape {
23
+ func path(in rect: CGRect) -> Path {
24
+ let bezierPath = UIBezierPath()
25
+ bezierPath.move(to: CGPoint(x: 53.22, y: 4.36))
26
+ for (to, controlPoint1, controlPoint2) in [(CGPoint(x: 60.83, y: 13.06), CGPoint(x: 57.76, y: 7.77), CGPoint(x: 60.14, y: 11.68)), (CGPoint(x: 68.43, y: 22.84), CGPoint(x: 63, y: 17.4), CGPoint(x: 65.05, y:20.96)), (CGPoint(x: 75.16, y: 23.98), CGPoint(x: 70.49, y: 23.98), CGPoint(x: 75.16, y: 23.98))] {
27
+ bezierPath.addCurve(to: to, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
28
+ }
29
+ bezierPath.addLine(to: CGPoint(x: 0.84, y: 23.98))
30
+ for (to, controlPoint1, controlPoint2) in [(CGPoint(x: 7.57, y: 22.84), CGPoint(x: 0.84, y: 23.98), CGPoint(x: 5.51, y: 23.98)), (CGPoint(x: 15.17, y: 13.06), CGPoint(x: 10.95, y: 20.96), CGPoint(x: 13, y: 17.4)), (CGPoint(x: 22.78, y: 4.36), CGPoint(x: 15.86, y: 11.68), CGPoint(x: 18.24, y: 7.77)), (CGPoint(x: 36.38, y: -0), CGPoint(x: 27.58, y: 0.77), CGPoint(x: 33.55, y: 0.1)), (CGPoint(x: 38, y: 0), CGPoint(x: 37.39, y: -0.04), CGPoint(x: 38, y: 0)), (CGPoint(x: 53.22, y: 4.36), CGPoint(x: 38, y: 0), CGPoint(x: 46.7, y: -0.53))] {
31
+ bezierPath.addCurve(to: to, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
32
+ }
33
+ bezierPath.close()
34
+ return Path(bezierPath.cgPath)
35
+ }
36
+ }
37
+ struct TabItemDescription {
38
+ var imageName: String
39
+ var title: String
40
+ func iconView(_ foregroundColor: Color) -> some View { Image(systemName: imageName).font(.system(size: 24)).foregroundColor(foregroundColor) }
41
+ func labelView(_ foregroundColor: Color) -> some View { Text(title).font(.system(size: 9, weight: .bold)).foregroundColor(foregroundColor) }
42
+ }
43
+ enum Defs {
44
+ static let tabItems: [TabItemDescription] = [.init(imageName: "house.fill", title: "HOME"),
45
+ .init(imageName: "magnifyingglass", title: "SEARCH"),
46
+ .init(imageName: "person.fill", title: "PROFILE"),
47
+ .init(imageName: "magnifyingglass", title: "SEARCH"),
48
+ .init(imageName: "person.fill", title: "PROFILE")]
49
+ static let accentColor = Color(UIColor(red: 0, green: 0.251, blue: 0.6588, alpha: 1)); static let backgroundColor = Color(UIColor(red: 0.945, green: 0.969, blue: 0.984, alpha: 1.000))
50
+ static let topFrameSize = CGSize(width: 75, height: 24)
51
+ static let tabbarHeight = CGFloat(49)
52
+ static let bottomSafeArea = CGFloat(40)
53
+ static let iconCircleEdge = CGFloat(40)
54
+ static let labelOffset = CGSize(width: 0, height: 32)
55
+ static let bottomSafeAreaOffset = CGSize(width: 0, height: Defs.bottomSafeArea * 0.5)
56
+ }
57
+
58
+ struct ContentView: View {
59
+ @State var selectedIndex = 0
60
+ var body: some View {
61
+ ZStack {
62
+ Text("Welcome").font(.largeTitle) // main contents example
63
+ GeometryReader { proxy in
64
+ VStack {
65
+ Spacer()
66
+ HStack(alignment: .bottom, spacing: 0){
67
+ ForEach(0..<Defs.tabItems.count) { (index) in
68
+ VStack(spacing: 0) {
69
+ Spacer()
70
+ Rectangle()
71
+ .foregroundColor( .clear )
72
+ .frame(width: Defs.iconCircleEdge, height: Defs.iconCircleEdge)
73
+ .overlay(
74
+ ZStack {
75
+ Defs.tabItems[index].iconView(.white).opacity(self.selectedIndex == index ? 1.0 : 0.0)
76
+ Defs.tabItems[index].iconView(Defs.accentColor).opacity(self.selectedIndex != index ? 1.0 : 0.0)
77
+ }
78
+ )
79
+ .background(
80
+ ZStack {
81
+ Defs.tabItems[index].labelView(.black).opacity(self.selectedIndex == index ? 1.0 : 0.0)
82
+ Defs.tabItems[index].labelView(.clear).opacity(self.selectedIndex != index ? 1.0 : 0.0)
83
+ }.offset(Defs.labelOffset)
84
+ )
85
+ Rectangle()
86
+ .foregroundColor(.clear)
87
+ .frame(height: self.selectedIndex == index ? 26 : 5)
88
+ }
89
+ .frame(width: proxy.size.width * 0.2)
90
+ .contentShape( Rectangle() )
91
+ .onTapGesture {
92
+ withAnimation(.interactiveSpring(response: 0.4, dampingFraction: 0.7) ) { self.selectedIndex = index }
93
+ }
94
+ }
95
+ }
96
+ .background(
97
+ Rectangle()
98
+ .frame(height: Defs.topFrameSize.height + Defs.tabbarHeight + Defs.bottomSafeArea)
99
+ .overlay(
100
+ Circle()
101
+ .foregroundColor(Defs.accentColor)
102
+ .frame(width: Defs.iconCircleEdge, height: Defs.iconCircleEdge)
103
+ .offset(CGSize(width: CGFloat(self.selectedIndex - 2) * (proxy.size.width * 0.2), height: -29))
104
+ )
105
+ .foregroundColor(.white)
106
+ .offset(Defs.bottomSafeAreaOffset)
107
+ .mask(
108
+ VStack(spacing: 0) {
109
+ TopFrameView()
110
+ .frame(width: Defs.topFrameSize.width, height: Defs.topFrameSize.height)
111
+ .offset(CGSize(width: CGFloat(self.selectedIndex - 2) * (proxy.size.width * 0.2), height: 0))
112
+ Rectangle()
113
+ .frame(height: Defs.tabbarHeight + Defs.bottomSafeArea)
114
+ }.offset(Defs.bottomSafeAreaOffset)
115
+ )
116
+ .shadow(color: Color.black.opacity(0.3) , radius: 15, x: 0, y: 0)
117
+ )
118
+ .frame(height: Defs.topFrameSize.height + Defs.tabbarHeight)
119
+ }
120
+ }
121
+ .ignoresSafeArea(edges: [.trailing, .leading])
122
+ }
123
+ .background(Defs.backgroundColor)
124
+ }
125
+ }
126
+
127
+ struct TopFrameView_Previews: PreviewProvider {
128
+ static var previews: some View {
129
+ ContentView()
130
+ }
131
+ }
132
+
133
+ ```