【问题标题】:What is the "View.onTouchListener" equivalent in Jetpack Compose? I need the touch coordinatesJetpack Compose 中的“View.onTouchListener”等价物是什么?我需要触摸坐标
【发布时间】:2021-08-07 02:46:45
【问题描述】:

这是我们目前在典型视图中使用的:https://developer.android.com/reference/android/view/View.OnTouchListener

在 Jetpack Compose 中是否有等价物?

【问题讨论】:

    标签: android android-jetpack android-jetpack-compose


    【解决方案1】:

    在 compose beta-05 版本中,您可以使用:

    Text("Your Composable", modifier = Modifier.pointerInput(Unit) {
        detectTransformGestures { centroid, pan, zoom, rotation ->
        }
        // or
        detectDragGestures { change, dragAmount ->  }
        // or
        detectTapGestures(
            onPress = { offset ->  },
            onDoubleTap = { offset -> },
            onLongPress = { offset -> },
            onTap = { offset ->  }
        )
        // or other similar...
    })
    

    【讨论】:

    • 我认为这已被弃用
    【解决方案2】:

    使用1.0.0,您可以使用PointerInput 模组

    例如你可以使用detectTapGestures:

    Modifier.pointerInput(Unit) {
        detectTapGestures(
            onPress = {/* Called when the gesture starts */ },
            onDoubleTap = { /* Called on Double Tap */ },
            onLongPress = { /* Called on Long Press */ },
            onTap = { /* Called on Tap */ }
        )
    }
    

    detectDragGestures:

    Box(
        Modifier
            .pointerInput(Unit) {
                detectDragGestures { change, dragAmount ->
                    change.consumeAllChanges()
                    //...
                }
            }
    )
    

    您还可以使用一些修饰符,例如:.scrollable.clickable.draggable.swipeable

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多