我正在尝试在 Compose 中检测滑动方向。我为此使用了可拖动的修饰符。但是draggable 只允许检测一个方向(垂直或水平)。我想检测所有方向(左、右、上、下)的滑动。谁能帮助我我该怎么做?谢谢!
最佳答案
与 1.0.0
您可以使用 pointerInput
使用 detectDragGestures
控制拖动手势的修饰符功能。
就像是:
Box(modifier = Modifier.fillMaxSize()) {
var offsetX by remember { mutableStateOf(0f) }
var offsetY by remember { mutableStateOf(0f) }
Box(
Modifier
.offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
.size(100.dp, 100.dp)
.background(Color.Blue)
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
change.consumeAllChanges()
val (x,y) = dragAmount
when {
x > 0 ->{ /* right */ }
x < 0 ->{ /* left */ }
}
when {
y > 0 -> { /* down */ }
y < 0 -> { /* up */ }
}
offsetX += dragAmount.x
offsetY += dragAmount.y
}
}
)
}
关于android - 检测 Jetpack Compose 上的滑动方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65573008/