记录一下,横滚选中的样式
let array = ["推荐", "手机", "箱包", "盲盒", "手办", "双十一转卖", "童裝"]
struct ContentView: View {
@State var typeSwitch = 0
var body: some View {
VStack {
ScrollView(.horizontal,showsIndicators: false) {
VStack(alignment: .weirdAlignment,spacing: 0) {
HStack(spacing: 16) {
ForEach(array.indices, id: \.self) { i in
Group {
if i == typeSwitch {
Text(array[i])
.foregroundColor(.black)
.font(Font.system(size: 21,weight: .bold))
.alignmentGuide(.weirdAlignment) { d in
d[HorizontalAlignment.center]
}
.transition(AnyTransition.identity)
} else {
Button(array[i]) {
withAnimation {
typeSwitch = i
}
}
.foregroundColor(.gray)
.font(Font.system(size: 18,weight: .regular))
.transition(AnyTransition.identity)
}
}
}
}.padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
Image("lineIcon").alignmentGuide(.weirdAlignment) { d in
d[HorizontalAlignment.center]
}.padding(EdgeInsets(top: 0, leading: 0, bottom: 4, trailing: 0))
}
}
}
.padding()
}
}
extension HorizontalAlignment {
private enum WeirdAlignment: AlignmentID {
static func defaultValue(in context: ViewDimensions) -> CGFloat {
return 0
}
}
static let weirdAlignment = HorizontalAlignment(WeirdAlignment.self)
}