verticalDrag

Function

Common
suspend fun AwaitPointerEventScope.verticalDrag(
    pointerId: PointerId,
    onDrag: (PointerInputChange) -> Unit,
): Boolean

Reads vertical position change events for pointerId and calls onDrag for every change in position. If pointerId is raised, a new pointer is chosen from those that are down and if none exist, the method returns. This does not wait for touch slop

Example Usage:

Returns

true if the vertical drag completed normally or false if the drag motion was canceled by another gesture detector consuming position change events.

Code Examples

VerticalDragSample

@Composable
fun VerticalDragSample() {
    val offsetX = remember { mutableStateOf(0f) }
    val offsetY = remember { mutableStateOf(0f) }
    var height by remember { mutableStateOf(0f) }
    Box(Modifier.fillMaxSize().onSizeChanged { height = it.height.toFloat() }) {
        Box(
            Modifier.offset { IntOffset(offsetX.value.roundToInt(), offsetY.value.roundToInt()) }
                .fillMaxWidth()
                .height(50.dp)
                .background(Color.Blue)
                .pointerInput(Unit) {
                    awaitEachGesture {
                        val down = awaitFirstDown()
                        val change =
                            awaitVerticalTouchSlopOrCancellation(down.id) { change, over ->
                                val originalY = offsetY.value
                                val newValue =
                                    (originalY + over).coerceIn(0f, height - 50.dp.toPx())
                                change.consume()
                                offsetY.value = newValue
                            }
                        if (change != null) {
                            verticalDrag(change.id) {
                                val originalY = offsetY.value
                                val newValue =
                                    (originalY + it.positionChange().y).coerceIn(
                                        0f,
                                        height - 50.dp.toPx(),
                                    )
                                it.consume()
                                offsetY.value = newValue
                            }
                        }
                    }
                }
        )
    }
}