awaitTouchSlopOrCancellation
Function
Common
suspend fun AwaitPointerEventScope.awaitTouchSlopOrCancellation(
pointerId: PointerId,
onTouchSlopReached: (change: PointerInputChange, overSlop: Offset) -> Unit,
): PointerInputChange?
Waits for drag motion to pass touch slop
, using pointerId
as the
pointer to examine. If pointerId
is raised, another pointer from those that are down will be
chosen to lead the gesture, and if none are down, null
is returned. If pointerId
is not down
when awaitTouchSlopOrCancellation
is called, then null
is returned.
onTouchSlopReached
is called after ViewConfiguration.touchSlop
motion in the any direction
with the change that caused the motion beyond touch slop and the Offset
beyond touch slop that
has passed. onTouchSlopReached
should consume the position change if it accepts the motion. If
it does, then the method returns that PointerInputChange
. If not, touch slop detection will
continue.
Example Usage:
Returns
The PointerInputChange that was consumed in onTouchSlopReached or null if all pointers are raised before touch slop is detected or another gesture consumed the position change. |
Code Examples
AwaitDragOrCancellationSample
@Composable
fun AwaitDragOrCancellationSample() {
val offsetX = remember { mutableStateOf(0f) }
val offsetY = remember { mutableStateOf(0f) }
var size by remember { mutableStateOf(Size.Zero) }
Box(Modifier.fillMaxSize().onSizeChanged { size = it.toSize() }) {
Box(
Modifier.offset { IntOffset(offsetX.value.roundToInt(), offsetY.value.roundToInt()) }
.size(50.dp)
.background(Color.Blue)
.pointerInput(Unit) {
awaitEachGesture {
val down = awaitFirstDown()
var change =
awaitTouchSlopOrCancellation(down.id) { change, over ->
val original = Offset(offsetX.value, offsetY.value)
val summed = original + over
val newValue =
Offset(
x = summed.x.coerceIn(0f, size.width - 50.dp.toPx()),
y = summed.y.coerceIn(0f, size.height - 50.dp.toPx()),
)
change.consume()
offsetX.value = newValue.x
offsetY.value = newValue.y
}
while (change != null && change.pressed) {
change = awaitDragOrCancellation(change.id)
if (change != null && change.pressed) {
val original = Offset(offsetX.value, offsetY.value)
val summed = original + change.positionChange()
val newValue =
Offset(
x = summed.x.coerceIn(0f, size.width - 50.dp.toPx()),
y = summed.y.coerceIn(0f, size.height - 50.dp.toPx()),
)
change.consume()
offsetX.value = newValue.x
offsetY.value = newValue.y
}
}
}
}
)
}
}