Skip to content

Instantly share code, notes, and snippets.

@fikrihkll
Created June 23, 2024 10:41
Show Gist options
  • Save fikrihkll/33e459a087535fb0ca07214ad0b975c1 to your computer and use it in GitHub Desktop.
Save fikrihkll/33e459a087535fb0ca07214ad0b975c1 to your computer and use it in GitHub Desktop.
Swipe-to-Close Gesture Jetpack Compose
@Composable
fun DetailScreen(
onNavigatePop: () -> Unit,
) {
val imageYOffset = remember {
Animatable(0f)
}
val scaleFactor = remember {
Animatable(1f)
}
val scope = rememberCoroutineScope()
val screenHeightDp = LocalConfiguration.current.screenHeightDp
val minOffsetToClose = screenHeightDp / 5
val swipeToCloseGesture = Modifier.pointerInput(Unit) {
detectDragGestures(
onDrag = { _, dragAmount ->
val dragY = dragAmount.y
scope.launch {
launch {
imageYOffset.animateTo(imageYOffset.value + (dragY))
}
launch {
scaleFactor.animateTo((1f - (imageYOffset.value / 1000f)).coerceIn(0.5f, 1f))
}
}
},
onDragEnd = {
if (imageYOffset.value >= minOffsetToClose) {
onNavigatePop.invoke()
} else {
scope.launch {
launch {
imageYOffset.animateTo(0f)
}
launch {
scaleFactor.animateTo(1f)
}
}
}
}
)
}
Surface(
modifier = Modifier
.background(Color.Transparent)
.graphicsLayer {
scaleX = scaleFactor.value
scaleY = scaleFactor.value
}
.offset(0.dp, imageYOffset.value.dp)
.then(swipeToCloseGesture)
) {
AsyncImage(
modifier = Modifier
.fillMaxSize(),
model = ImageRequest.Builder(LocalContext.current)
.data("https://www.kevinandamanda.com/wp-content/uploads/2012/05/cinque-terre-301.jpg")
.crossfade(true)
.build(),
placeholder = painterResource(
id = R.drawable.ic_launcher_background
),
contentDescription = "Cinque Terre Image"
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment