用户捏时如何防止拖动

How to prevent drag when user is pinching

所以基本上我有一个 ImageView 可以 zoomchange 它的垂直偏移,但是当我想缩放时它真的很难做到,因为偏移仍在 变化捏的时候

更多内容在下面的代码中:

modifier = Modifier
    .fillMaxSize()
    .background(Color.Black)
    .pointerInput(imageViewModel.currentZoom) {
        if (imageViewer.currentZoom == 1f) {
            detectDragGestures(
                onDragStart = { imageViewModel.isPressed = true },
                onDragEnd = { imageViewModel.isPressed = false },
                onDragCancel = { imageViewModel.isPressed = false },
                onDrag = { change, dragAmount ->
                    change.consumeAllChanges()

                    // TODO: if (fingerCount == 1) or when there is no multitouch detected
                    // change the offset by drag amount
                    imageViewModel.offsetY += dragAmount.y
                }
            )
        }
    }

我自己找到了解决方案,这是修订版:

val fingerCount = remember { 0f }

modifier = Modifier
    .fillMaxSize()
    .background(Color.Black)
    .pointerInput(Unit) {
        forEachGesture {
            val context = currentCoroutineContext()

            awaitPointerEventScope {
                do {
                    val event = awaitPointerEvent()

                    fingerCount = event.changes.size
                } while (event.changes.any { it.pressed } && context.isActive)
            }
        }
    }
    .pointerInput(
        key1 = imageViewModel.currentZoom,
        key2 = fingerCount,
    ) {
        if (imageViewModel.offsetY != 0f || fingerCount > 1) {
            imageViewModel.isPressed = false
        }

        if (imageViewer.currentZoom == 1f) {
            detectDragGestures(
                onDragStart = { imageViewModel.isPressed = true },
                onDragEnd = { imageViewModel.isPressed = false },
                onDragCancel = { imageViewModel.isPressed = false },
                onDrag = { change, dragAmount ->
                    if ((fingerCount == 1 || imageViewModel.offsetY != 0f) && sheetState.targetValue == ModalBottomSheetValue.Hidden) {
                        change.consumeAllChanges()
                        imageViewModel.offsetY += dragAmount.y
                    }
                }
            )
        }
    }
    .offset {
        IntOffset(
            x = 0,
            y =
            if (imageViewModel.isPressed)
                imageViewModel.offsetY.roundToInt()
            else
                animatedOffsetY.roundToInt(),
        )
    },