(Deaktivierung desselben Elements ist akzeptabel animiert)
minimal reproduzierbares Beispiel:
Code: Select all
struct ChipsView: View {
let title: String
@Binding var selectedString: String
private var isSelected: Bool { selectedString == title }
private let selectedColor = Color.red
private let unselectedColor = Color.gray
private let selectedFont: Font.Weight = .bold
private let unselectedFont: Font.Weight = .light
var body: some View {
Text(title)
.font(.system(
size: 56,
weight: isSelected ? selectedFont : unselectedFont
))
.foregroundColor(isSelected ? selectedColor : unselectedColor)
.contentShape(Rectangle())
.onTapGesture {
withAnimation(.bouncy(duration: 0.3)) {
selectedString = isSelected ? "" : title
}
}
}
}
struct ChipsView_CustomPreview: View {
@State var selectedString: String = ""
var body: some View {
HStack {
ForEach(["11","22","33","44","55"], id: \.self) { str in
ChipsView(title: str, selectedString: $selectedString)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.black)
}
}
#Preview {
ChipsView_CustomPreview()
}