Slide 14
Slide 14 text
suspend fun PointerInputScope.detectTransformGestures(
onGestureStart: (GestureType) -> Unit,
onGestureEnd: (GestureType) -> Unit,
onDrag: (dragAmount: Offset) -> Unit,
onZoom: (zoom: Float) -> Unit,
) {
forEachGesture {
awaitPointerEventScope {
awaitFirstDown(requireUnconsumed = false)
val gestureType = detectGesture() ?: return@awaitPointerEventScope
onGestureStart(gestureType)
do {
val event = awaitPointerEvent()
when (gestureType) {
GestureType.DRAG -> {
val dragChange = event.calculatePan()
if (dragChange != Offset.Zero) onDrag(dragChange)
}
GestureType.ZOOM -> {
val zoomChange = event.calculateZoom()
if (zoomChange != 1f) onZoom(zoomChange)
}
}
event.changes.fastForEach { if (it.positionChanged()) it.consume() }
} while (event.changes.fastAny { it.pressed })
onGestureEnd(gestureType)
}
}
}
private suspend fun AwaitPointerEventScope.detectGesture(): GestureType? {
var zoom = 1f
var pan = Offset.Zero
val touchSlop = viewConfiguration.touchSlop
var gestureType: GestureType? = null
do {
val event = awaitPointerEvent()
zoom *= event.calculateZoom()
pan += event.calculatePan()
val zoomMotion = abs(1 - zoom) * event.calculateCentroidSize(useCurrent = false)
val panMotion = pan.getDistance()
if (zoomMotion > touchSlop) {
gestureType = GestureType.ZOOM
break
}
if (panMotion > touchSlop) {
gestureType = GestureType.DRAG
break
}
} while (event.changes.fastAny { it.pressed })
return gestureType
}
enum class GestureType {
DRAG, ZOOM
}
ΞϓϦͷཁ݅Λຬͨͤͳ͍ͷͰdetectؔΛࣗͰॻ͍ͨ
※Ջͳͱ͖ʹݟͯͶ https://bit.ly/composegesture