alexsullivan114
03/09/2021, 12:40 PMtranslationY
value for both the scrolling and the tick. Right now, when I toggle between scrolling mode or ticking animation mode the composable kind of swaps between the two different translationY values. I'd like to remember the old translationY and increment from there but I'm not sure how to make that happen...I've added the relevant code to this thread.alexsullivan114
03/09/2021, 12:42 PMfun VerticalDigits(focusedDigit: Int, scrollable: Boolean) {
val translationAnimation: Float by animateFloatAsState(
targetValue = verticalDigitTranslation(focusedDigit, heightPixels)
)
var scrollableTranslation by remember { mutableStateOf(translationAnimation) }
val heightModifier = Modifier.height(height)
val translation = if (scrollable) scrollableTranslation else translationAnimation
return Column(
modifier = Modifier
.graphicsLayer(translationY = translationY)
.scrollable(
orientation = Orientation.Vertical, state = rememberScrollableState(
consumeScrollDelta = { delta ->
scrollableTranslation += delta
delta
})
)
) { ... }
alexsullivan114
03/09/2021, 12:42 PMFilip Wiesner
03/09/2021, 12:50 PMalexsullivan114
03/09/2021, 1:09 PMFilip Wiesner
03/09/2021, 1:23 PMVerticalDigits
display it and return changes via function argument '`(delta: Float) -> Unit`'
I don't know how focusedDigit
is calculated or what verticalDigitTranslation
does but here is an example:
fun Root() {
var translation by remember { mutableState(0f) } // change on scroll
val focusedDigit = calculateFocusedDigit(translation)
VerticalDigits(focusedDigit, scrollable) { translation += it }
}
fun VerticalDigits(focusedDigit: Int, scrollable: Boolean, onTranslationChange: (Float) -> Unit) {
val translationAnimation by animateFloatAsState(verticalDigitTranslation(focusedDigit, heightPixels))
// The rest of the function
}
(I haven't tested it)Filip Wiesner
03/09/2021, 1:36 PMalexsullivan114
03/09/2021, 1:56 PMDoris Liu
03/09/2021, 9:37 PMAnimatable
for this type of use cases: you can animate using Animatable.animateTo
. When scrolling happens, call Animatable.snapTo(...)
to stop the animation and update the scroll value. I'd do something like this: (not tested)
@Composable
fun VerticalDigits(focusedDigit: Int, scrollable: Boolean) {
val translationAnimation = remember {
Animatable(verticalDigitTranslation(focusedDigit, heightPixels))
}
LaunchedEffect(focusedDigit, heightPixels) {
translationAnimation.animateTo(
targetValue = verticalDigitTranslation(focusedDigit, heightPixels)
)
}
val scope = rememberCoroutineScope()
...
Modifier
.scrollable(
orientation = Orientation.Vertical, state = rememberScrollableState(
consumeScrollDelta = { delta ->
scope.launch {
translationAnimation.snapTo(translationAnimation.value + delta)
}
delta
})
).graphicsLayer {
translationY = translationAnimation.value
}
}
Doris Liu
03/09/2021, 9:42 PMalexsullivan114
03/14/2021, 1:00 AM