在JetpackCompose上检测滑动方向
我正在尝试在 Compose 中检测滑动方向。我为此使用了可拖动修饰符。但可拖动只允许检测一个方向(垂直或水平)。我想检测所有方向(左、右、上、下)的滑动。谁能帮助我我该怎么做?谢谢!
回答
有了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
}
}
)
}