Created
June 15, 2022 10:24
-
-
Save cp-radhika-s/4c36084e087a03841a62e34eb3c6e0c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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