Skip to content
Snippets Groups Projects
Commit 34723b8b authored by Fodor Patrik's avatar Fodor Patrik :ghost:
Browse files

Swipe animation

parent 86f78192
No related branches found
No related tags found
No related merge requests found
package hu.bme.kszk.szobatarsch.composable package hu.bme.kszk.szobatarsch.composable
import androidx.compose.runtime.Composable import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@OptIn(ExperimentalComposeUiApi::class, ExperimentalMaterial3Api::class)
@Composable @Composable
fun FindPeople() { fun FindPeople() {
val items = remember { mutableStateListOf<Int>() }
LaunchedEffect(Unit) {
(1..5).forEach { items += it }
}
val width = LocalConfiguration.current.screenWidthDp.dp
val density = LocalDensity.current
fun Float.pxToDp() = with(density) { this@pxToDp.toDp() }
val edge = width + 100.dp
val sens = width / 2
val degPerDp = 20 / width.value
fun Dp.getRotation() = value * degPerDp
Box() {
items.forEach {
var offset by remember { mutableStateOf(0.dp) }
val animatedOffset by animateDpAsState(
targetValue = offset,
finishedListener = { off ->
if (off == edge) {
items -= it
} else if (off == -edge) {
items -= it
}
},
)
Card(
modifier = Modifier
.pointerInput(Unit) {
detectDragGestures(
onDragEnd = {
offset = if (offset > sens) {
edge
} else if (offset < -sens) {
-edge
} else {
0.dp
}
},
onDrag = { change, amount ->
offset += amount.x.pxToDp()
change.consume()
},
)
}
.offset(x = animatedOffset)
.rotate(animatedOffset.getRotation())
.fillMaxSize()
.padding(8.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 10.dp)
) {
Text(it.toString())
}
}
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment