当scrollview
被滚动经过给定ID的元素时,有什么方法可以得到通知吗?
例如:
@State var selectedFoodTypeId: UUID? = nil
HStack {
ForEach(foodTypes) { foodType in
Text(foodType)
.foregroundColor(selectedFoodTypeId == foodType.id ? .red : .black)
}
}
ScrollView {
ForEach(foodTypesAndRestaurants) { item in
Text(item.foodType)
.id(item.foodType.id)
// When ScrollView was just scrolled here, I would like to do the following:
// withAnimation {
// selectedFoodTypeId = item.foodType.id
// }
ForEach(item.restaurants) { restaurant in
Text(restaurant)
}
}
}
您可以使用.onexprece(perform:{})
或.onexprece(perform:{})
以及附加逻辑。
Text(item.foodType)
.id(item.foodType.id)
.onAppear(perform: { })