detectVerticalDragGestures
suspend fun PointerInputScope.detectVerticalDragGestures(
onDragStart: (Offset) -> Unit = {},
onDragEnd: () -> Unit = {},
onDragCancel: () -> Unit = {},
onVerticalDrag: (change: PointerInputChange, dragAmount: Float) -> Unit,
)
Gesture detector that waits for pointer down and touch slop in the vertical direction and then
calls onVerticalDrag
for each vertical drag event. It follows the touch slop detection of
awaitVerticalTouchSlopOrCancellation
, but will consume the position change automatically once
the touch slop has been crossed.
onDragStart
called when the touch slop has been passed and includes an Offset
representing
the last known pointer position relative to the containing element. The Offset
can be outside
the actual bounds of the element itself meaning the numbers can be negative or larger than the
element bounds if the touch target is smaller than the
ViewConfiguration.minimumTouchTargetSize
.
onDragEnd
is called after all pointers are up and onDragCancel
is called if another gesture
has consumed pointer input, canceling this gesture.
This gesture detector will coordinate with detectHorizontalDragGestures
and
awaitHorizontalTouchSlopOrCancellation
to ensure only vertical or horizontal dragging is
locked, but not both.
Example Usage:
Code Examples
DetectVerticalDragGesturesSample
@Composable
fun DetectVerticalDragGesturesSample() {
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) {
detectVerticalDragGestures { _, dragAmount ->
val originalY = offsetY.value
val newValue = (originalY + dragAmount).coerceIn(0f, height - 50.dp.toPx())
offsetY.value = newValue
}
}
)
}
}