Slide 55
Slide 55 text
Fling and Back
// 3. How?
val decay = rememberSplineBasedDecay()
fun doFlingMove(velocity: Velocity) {
// 1. Calculate target offset based on velocity
val velocityOffset = Offset(velocity.x / 2f, velocity.y / 2f)
val targetOffset = decay.calculateTargetValue(
typeConverter = Offset.VectorConverter,
initialValue = translation.value,
initialVelocity = velocityOffset,
)
// 2. If the target offset is within bounds, animate to it
if (isTargetInBounds(targetOffset, screenSize)) {
coroutineScope.launch {
translation.animateDecay(velocityOffset, decay)
}
}
else { // 3. If not, animate to farthest point within bounds and then animate back to center
coroutineScope.launch {
val adjustedOffset = calculateBoundedOffset(targetOffset, screenSize)
translation.animateTo(adjustedOffset) // Animate to farthest point
translation.animateTo(Offset(0f, 0f), ..) // Animate back to center
}
}
}