在Android编写pointerInput我怎么能使用关键参数

7hiiyaii  于 2023-03-21  发布在  Android
关注(0)|答案(1)|浏览(121)

我有下面的代码.

@Composable
fun MainCompose(trigger: Int) {
    var trigger by remember { mutableStateOf(0) }
    val onDrag = { trigger += 1 }
    MyPointInput(trigger = trigger, onDrag)
}
@Composable
fun MyPointInput(trigger: Int, onDrag: () -> Unit) {
    Box(modifier = Modifier
        .background(Color.Green)
        .size(300.dp)
        .pointerInput(trigger) {
            var a = 0
            detectTransformGestures { centroid, pan, zoom, rotation ->
                println("trigger: $trigger with $a")
                a += 1
                onDrag()
            }
        })
}

当我开始拖动时,我可以看到控制台中的数字1,2,3 ...。
但是如果触发数改变,则打印停止并且不再开始。
文件上说
将捕获的值指定为关键参数将导致块在值更改时取消并从头重新启动:
为什么我的代码没有重新启动

wqnecbli

wqnecbli1#

如果使用情况有问题,您正在隐藏MainCompose(trigger: Int)的参数,则仅使用var trigger by remember { mutableStateOf(0) }。如果您希望将触发值重置为参数值,则可以将其更新为

@Composable
fun MainCompose(initialValue: Int) {
    var trigger by remember(initialValue) { mutableStateOf(initialValue) }
    val onDrag = { trigger += 1 }
    Text("Trigger: $trigger")
    MyPointInput(trigger = trigger, onDrag)
}

通过更改此值,您将在每次onDrag调用时更新触发器,并重置为从使用initialValue参数调用MainCompose的函数发送的值。
您可能还希望在每次initialValue更改时重置Modifer.pointerInput(),而不是将其本身触发为

MyPointInput(trigger = initialValue, onDrag)

只有当新值通过MainCompose发送时才会重置。2使用触发器作为参数更改触发器更新的时刻,并导致前一个立即丢弃,当然假设这不是预期行为。
输出将为

实施情况如下

@Preview
@Composable
private fun Test() {

    var value by remember {
        mutableStateOf(0)
    }
    Column() {

        MainCompose(value)
        Spacer(modifier = Modifier.height(20.dp))
        Button(onClick = {
            value++
        }) {
            Text(text = "Click")
        }
    }
}

@Composable
fun MainCompose(initialValue: Int) {
    var trigger by remember(initialValue) { mutableStateOf(initialValue) }
    val onDrag = { trigger += 1 }
    Text("Trigger: $trigger")
    MyPointInput(trigger = initialValue, onDrag)
}

@Composable
fun MyPointInput(trigger: Int, onDrag: () -> Unit) {
    Box(modifier = Modifier
        .background(Color.Green)
        .size(300.dp)
        .pointerInput(trigger) {
            var a = 0
            detectTransformGestures { centroid, pan, zoom, rotation ->
                println("trigger: $trigger with $a")
                a += 1
                onDrag()
            }
        })
}

其中一些日志

I  trigger: 2 with 34
 I  trigger: 2 with 35
 I  trigger: 2 with 36
 I  trigger: 3 with 0
 I  trigger: 3 with 1
 I  trigger: 3 with 2
 I  trigger: 3 with 3
 I  trigger: 3 with 4
 I  trigger: 3 with 5
 I  trigger: 3 with 6
 I  trigger: 3 with 7
 I  trigger: 3 with 8
 I  trigger: 3 with 9
 I  trigger: 3 with 10
 I  trigger: 3 with 11
 I  trigger: 3 with 12
 I  trigger: 3 with 13
 I  trigger: 3 with 14
 I  trigger: 3 with 15
 I  trigger: 3 with 16
 I  trigger: 3 with 17
 I  trigger: 3 with 18
 I  trigger: 3 with 19
 I  trigger: 3 with 20
 I  trigger: 3 with 21
 I  trigger: 3 with 22
 I  trigger: 3 with 23
 I  trigger: 3 with 24
 I  trigger: 3 with 25
 I  trigger: 3 with 26
 I  trigger: 3 with 27
 I  trigger: 3 with 28
 I  trigger: 3 with 29
 I  trigger: 3 with 30
 I  trigger: 3 with 31
 I  trigger: 3 with 32
 I  trigger: 3 with 33
 I  trigger: 3 with 34
 I  trigger: 3 with 35
 I  trigger: 3 with 36
 I  trigger: 3 with 37
 I  trigger: 3 with 38
 I  trigger: 4 with 0
 I  trigger: 4 with 1
 I  trigger: 4 with 2
 I  trigger: 4 with 3
 I  trigger: 4 with 4
 I  trigger: 4 with 5
 I  trigger: 4 with 6
 I  trigger: 4 with 7
 I  trigger: 4 with 8
 I  trigger: 4 with 9
 I  trigger: 4 with 10
 I  trigger: 4 with 11
 I  trigger: 4 with 12
 I  trigger: 4 with 13
 I  trigger: 4 with 14
 I  trigger: 4 with 15
 I  trigger: 4 with 16
 I  trigger: 4 with 17
 I  trigger: 4 with 18
 I  trigger: 4 with 19
 I  trigger: 4 with 20
 I  trigger: 4 with 21
 I  trigger: 4 with 22
 I  trigger: 4 with 23

相关问题