Hi,
I have a code snippet as below that creates a random selection animation. I would like the animation gradually slows down instead of abruptly stopping when the specified duration is reached. I initially attempted to achieve this by using multiple timers with different intervals to represent various animation speeds and then using another timer to trigger these timers. However, I'm not feeling well with this approach and am seeking better suggestions and insights. Any comments and recommendations would be greatly appreciated.
@State private var timer: Timer?
@State private var elapsedTime: TimeInterval = 0
private let timeInterval: TimeInterval = 0.1
private let duration: TimeInterval = 10
func start() {
if timer == nil {
timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true) { _ in
if elapsedTime < duration {
elapsedTime += timeInterval
withAnimation(.spring(response: timeInterval)) {
// randomly select an item
}
} else {
timer?.invalidate()
timer = nil
elapsedTime = 0
}
}
timer?.fire()
}
}