Reputation: 656
I want change color of the Text item onTapGesture for simulate the selected item, when click the Text color should be Red and other Text Black.
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 10) {
ForEach(newsFeed.subcategoryListNews) { item in
Text(item.name)
.font(Font.custom("AvenirNextLTPro-Demi", size: 17))
.foregroundColor(Color.black)
.onTapGesture {
//Chage color and reset other Text, active red not active black
}
}
}
.padding(.top, 30)
.padding(.horizontal, 30)
}.padding(.bottom, 10)
Many thanks
Upvotes: 1
Views: 697
Reputation: 257729
It needs to conform your item type Equatable
(if it is not yet) then the following approach is possible
@State private var selectedItem: <Item_Type_Here>? = nil
...
ForEach(newsFeed.subcategoryListNews) { item in
Text(item.name)
.font(Font.custom("AvenirNextLTPro-Demi", size: 17))
.foregroundColor(self.selectedItem == item ? Color.red : Color.black))
.onTapGesture {
self.selectedItem = item
}
}
Upvotes: 1