Uiscrollview Swiftui Scrollview文本更改颜色OnTap手势
我想更改文本项ontapsignature的颜色,以模拟选定的项,单击时文本颜色应为红色,其他文本颜色应为黑色Uiscrollview Swiftui Scrollview文本更改颜色OnTap手势,uiscrollview,swiftui,Uiscrollview,Swiftui,我想更改文本项ontapsignature的颜色,以模拟选定的项,单击时文本颜色应为红色,其他文本颜色应为黑色 ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 10) { ForEach(newsFeed.subcategoryListNews) { item in Tex
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)
非常感谢它需要符合您的物品类型
equalable
(如果还没有),那么以下方法是可行的
@State private var selectedItem:?=无
...
ForEach(newsFeed.subcategoritylistnews){item in
文本(项目名称)
.font(font.custom(“AvenirNextLTPro-Demi”,大小:17))
.foregroundColor(self.selectedItem==项目?颜色。红色:颜色。黑色))
.ontapsigne{
self.selectedItem=项目
}
}