I am a beginner to SwiftUI and I am trying to scroll to positions on the screen based on a parameter, but I want to set a duration as well because now it scrolls fast, I want to slow it down but I have tried everything i can but does not seem to work
struct NftList: View {
var range: ClosedRange<Int>
@State private var targetIndex: Int?
@State private var scrollProxy: ScrollViewProxy?
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 0) {
ForEach(range, id: .self) { index in
Image("(index)")
.resizable()
.scaledToFit()
}
.onAppear() {
targetIndex = range.upperBound;
}
}
}
.scrollPosition(id: $targetIndex, anchor: .center)
.animation(.linear(duration: 0.2), value: targetIndex)
.onChange(of: targetIndex) { oldIndex, newIndex in
if newIndex == range.upperBound {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
targetIndex = range.lowerBound
}
} else if newIndex == range.lowerBound {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
targetIndex = range.upperBound
}
}
}
.rotationEffect(.radians(Double.pi * 1.96))
.frame(height: 130)
.scaleEffect(x: 1.08)
}
}