Skip to content

Instantly share code, notes, and snippets.

@cp-radhika-s
Created June 15, 2022 10:24
Show Gist options
  • Save cp-radhika-s/4c36084e087a03841a62e34eb3c6e0c2 to your computer and use it in GitHub Desktop.
Save cp-radhika-s/4c36084e087a03841a62e34eb3c6e0c2 to your computer and use it in GitHub Desktop.
@Composable
fun SpringRelease() {
val offsetY = remember { Animatable(0f) }
val scope = rememberCoroutineScope()
Box(
modifier = Modifier
.fillMaxSize()
.padding(20.dp),
contentAlignment = Alignment.TopCenter
) {
Circle(modifier = Modifier
.offset { IntOffset(0, offsetY.value.roundToInt()) }
.background(ThemeColor, CircleShape)
.pointerInput(Unit) {
forEachGesture {
awaitPointerEventScope {
//Detect a touch down event
awaitFirstDown()
do {
val event: PointerEvent = awaitPointerEvent()
event.changes.forEach { pointerInputChange: PointerInputChange ->
//Consume the change
scope.launch {
offsetY.snapTo(
offsetY.value + pointerInputChange.positionChange().y
)
}
}
} while (event.changes.any { it.pressed })
// Touch released - Action_UP
scope.launch {
offsetY.animateTo(
targetValue = 0f, spring(
dampingRatio = Spring.DampingRatioLowBouncy,
stiffness = StiffnessLow
)
)
}
}
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment