detectDragGesturesAfterLongPress
Function
Common
suspend fun PointerInputScope.detectDragGesturesAfterLongPress(
onDragStart: (Offset) -> Unit = {},
onDragEnd: () -> Unit = {},
onDragCancel: () -> Unit = {},
onDrag: (change: PointerInputChange, dragAmount: Offset) -> Unit,
)
Gesture detector that waits for pointer down and long press, after which it calls onDrag for
each drag event.
onDragStart called when a long press is detected 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 function will automatically consume all
the position change after the long press.
Example Usage:
Code Examples
DetectDragWithLongPressGesturesSample
@Composable
fun DetectDragWithLongPressGesturesSample() {
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) {
detectDragGesturesAfterLongPress { _, dragAmount ->
val original = Offset(offsetX.value, offsetY.value)
val summed = original + dragAmount
val newValue =
Offset(
x = summed.x.coerceIn(0f, size.width - 50.dp.toPx()),
y = summed.y.coerceIn(0f, size.height - 50.dp.toPx()),
)
offsetX.value = newValue.x
offsetY.value = newValue.y
}
}
)
}
}
