Gesture detector that waits for pointer down and long press, after which it calls [onDrag] for each drag event.
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
}
}
)
}
}