滑动冲突
来源:8-9 新一代后台任务管理组件WorkManager引导

慕设计2389733
2024-07-09
子view重写了detectTransformGestures导致当在重写了detectTransformGestures方法的view上滑动时候Column无法滑动,应该怎么解决?
代码:
外层的Column
Box(modifier = Modifier.fillMaxSize()){
val scrollState = rememberScrollState()
Column(Modifier
.verticalScroll(scrollState)
.padding(top = 50.dp)) {
ZoomPanBox()
// Box(modifier = Modifier.fillMaxWidth().height(200.dp)){
// TransformableSample()
// }
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
Text(text = “asasdasdasd”, fontSize = 30.sp)
}
}
重写了detectTransformGestures方法的子view
@SuppressLint(“ReturnFromAwaitPointerEventScope”)
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ZoomPanBox() {
var scale by remember { mutableStateOf(1f) }
var offset by remember { mutableStateOf(Offset.Zero) }
var isZooming by remember { mutableStateOf(true) }
var onFirstClick by remember { mutableStateOf(false) }
var changesSize by remember { mutableStateOf(0) }
val coroutineScope = rememberCoroutineScope()
val maxScale = 2f
val minScale = 1f
val textMeasurer = rememberTextMeasurer()
Box(
modifier = Modifier
.fillMaxWidth()
.height(502.dp)
.background(Color.Yellow)
.pointerInput(Unit) {
Log.e(“test”, “第二个pointerInput:$scale”)
detectTapGestures(
onDoubleTap = { position ->
Log.e(“test”, “双击”)
// Calculate new offset to zoom into the double-tap position
val newScale = if (scale > minScale) minScale else maxScale
val zoomFactor = newScale / scale
scale = newScale
val newOffsetX = ((position.x - size.width / 2) * zoomFactor) - (position.x - size.width / 2)
val newOffsetY = ((position.y - size.height / 2) * zoomFactor) - (position.y - size.height / 2)
val maxX = (size.width * (scale - 1)) / 2
val maxY = (size.height * (scale - 1)) / 2
offset = Offset(
x = (offset.x - newOffsetX).coerceIn(-maxX, maxX),
y = (offset.y - newOffsetY).coerceIn(-maxY, maxY)
)
}
)
}
.pointerInput(Unit) {
detectTransformGestures { _, pan, zoom, _ ->
Log.e("test", "detectTransformGestures 执行放大操作 zoom:" + zoom)
scale = (scale * zoom).coerceIn(1f, 5f)
val maxX = (size.width * (scale - 1) / 2)
val maxY = (size.height * (scale - 1) / 2)
val newOffsetX = (offset.x + pan.x).coerceIn(-maxX, maxX)
val newOffsetY = (offset.y + pan.y).coerceIn(-maxY, maxY)
offset = Offset(newOffsetX, newOffsetY)
}
// }
}
.clipToBounds()
) {
// Canvas(modifier = Modifier
// .size(300.dp)
// .graphicsLayer(
// scaleX = scale,
// scaleY = scale,
// translationX = offset.x,
// translationY = offset.y
// )
// ) {
// drawRect(
// color = Color.Blue,
// size = size
// )
// drawText(textMeasurer, “Hello”)
// }
Box(modifier = Modifier
.fillMaxWidth()
.height(502.dp)
.background(color = Color.Gray)
.graphicsLayer(
scaleX = scale,
scaleY = scale,
translationX = offset.x,
translationY = offset.y
)) {
Column {
Row {
ItemContent(Modifier
.fillMaxWidth()
.weight(1f)
.height(125.5.dp)
.background(color = Color.Red))
ItemContent(Modifier
.fillMaxWidth()
.weight(1f)
.height(125.5.dp)
.background(color = Color.Yellow))
ItemContent(Modifier
.fillMaxWidth()
.weight(1f)
.height(125.5.dp)
.background(color = Color.Green))
ItemContent(Modifier
.fillMaxWidth()
.weight(1f)
.height(125.5.dp)
.background(color = Color.Gray))
}
}
Canvas(modifier = Modifier.fillMaxSize()) {
// Log.e(“test”, “宽:” + (size.width.toString()) + “高:” + (size.width.toString()))
// drawLine(Color.Black, Offset(0f, 0f), Offset(size.width, size.height + 10), strokeWidth = 20f)
val canvasWidth = size.width
val canvasHeight = size.height
Log.e("test", "宽:" + (canvasWidth) + "高:" + (canvasHeight))
drawLine(
start = Offset(x = 0f, y = 0f),
end = Offset(x = canvasWidth, y = canvasHeight),
color = Color.Blue,
strokeWidth = 2f
)
}
}
}
}
@Composable
fun ItemContent(modifier: Modifier) {
Box(modifier = modifier) {
Text(text = “asdasdasda”)
}
}
1回答
-
LovelyChubby
2024-07-11
可以暴露一个方法让外部调用用于控制是否拦截手势00
相似问题