awaitLongPressOrCancellation

Function

Common
suspend fun AwaitPointerEventScope.awaitLongPressOrCancellation(
    pointerId: PointerId
): PointerInputChange?

Waits for a long press by examining pointerId.

If that pointerId is raised (that is, the user lifts their finger), but another finger (PointerId) is down at that time, another pointer will be chosen as the lead for the gesture, and if none are down, null is returned.

Example Usage:

Returns

The latest PointerInputChange associated with a long press or null if all pointers are raised before a long press is detected or another gesture consumed the change.

Code Examples

AwaitLongPressOrCancellationSample

/** Simple [awaitLongPressOrCancellation] demo. */
@Composable
fun AwaitLongPressOrCancellationSample() {
    var count by remember { mutableStateOf(0) }
    Column {
        Text("Long Press to increase count. Long Press count: $count")
        Box(
            Modifier.fillMaxSize()
                .wrapContentSize(Alignment.Center)
                .size(192.dp)
                .pointerInput(Unit) {
                    awaitEachGesture {
                        val down = awaitFirstDown(requireUnconsumed = false)
                        awaitLongPressOrCancellation(down.id)?.let { count++ }
                    }
                }
                .clipToBounds()
                .background(Color.Blue)
                .border(BorderStroke(2.dp, Color.Black))
        )
    }
}