Compose Modifier. swipeable的学习与理解

101 阅读1分钟
  //initialValue 设置的是state 比如这里设置的是false
                        //那么anchors 中的value就得是false,代表状态,另一个状态就是true
//                        var state = rememberSwipeableState(initialValue = false)
//                        val anchors = mapOf(200f to false, 100f to true)
                        //如果这里的initialValue 为0 那么anchors中对应的value就得是int
                        // 必须有一个值是 0 另一个是啥无所谓
                        //key 对应的是两个状态对应的偏移量,也就是 state.offset.value 的值
                        var state = rememberSwipeableState(initialValue = 0)
                        val anchors = mapOf(300f to 0, 0f to 1)
                        Box(modifier = Modifier
                            .size(100.dp)
                            .background(color = Color.Red)
                            //这个属性保证内容划出之后 不会展现出来
//                            .clipToBounds()
                            //对内部元素起作用
//                            .offset {
//                                IntOffset(
//                                    state.offset.value.toInt(), 0
//                                )
//                            }
                            .swipeable(
                                state = state,
                                anchors = anchors,
                                orientation = Orientation.Horizontal,
                                //thresholds 这个参数代表的意思是超过多少,松手后直接切换到下一个状态
                                //比如这里是0.3 总偏移量是300 300 * 0.3 = 90 只要滑动超过90,比如从3000 移动,当移动超过90,就直接变成0
                                thresholds = { _, _ -> FractionalThreshold(0.3f) },
                            )){
                            Log.i("cccccc","offset==${state.offset}")
                            Text(text = "${state.offset.value}", modifier = Modifier.graphicsLayer {
                                translationX = state.offset.value
                            })
                        }
                    }

加油.png