@@ -3,15 +3,21 @@ package org.zhiwei.compose.screen.gesture
33import android.icu.text.DecimalFormat
44import androidx.compose.foundation.Image
55import androidx.compose.foundation.background
6+ import androidx.compose.foundation.gestures.Orientation
67import androidx.compose.foundation.gestures.detectTransformGestures
8+ import androidx.compose.foundation.gestures.draggable
9+ import androidx.compose.foundation.gestures.rememberDraggableState
710import androidx.compose.foundation.layout.Box
811import androidx.compose.foundation.layout.Column
912import androidx.compose.foundation.layout.fillMaxSize
1013import androidx.compose.foundation.layout.fillMaxWidth
1114import androidx.compose.foundation.layout.height
15+ import androidx.compose.foundation.layout.offset
1216import androidx.compose.foundation.layout.padding
17+ import androidx.compose.foundation.layout.size
1318import androidx.compose.foundation.rememberScrollState
1419import androidx.compose.foundation.verticalScroll
20+ import androidx.compose.material.Switch
1521import androidx.compose.material3.Text
1622import androidx.compose.runtime.Composable
1723import androidx.compose.runtime.getValue
@@ -31,12 +37,14 @@ import androidx.compose.ui.input.pointer.pointerInput
3137import androidx.compose.ui.layout.ContentScale
3238import androidx.compose.ui.res.painterResource
3339import androidx.compose.ui.tooling.preview.Preview
40+ import androidx.compose.ui.unit.IntOffset
3441import androidx.compose.ui.unit.dp
3542import org.zhiwei.compose.R
3643import org.zhiwei.compose.ui.widget.Title_Desc_Text
3744import org.zhiwei.compose.ui.widget.Title_Sub_Text
3845import org.zhiwei.compose.ui.widget.Title_Text
3946import kotlin.math.cos
47+ import kotlin.math.roundToInt
4048import kotlin.math.sin
4149
4250/* *
@@ -53,9 +61,48 @@ internal fun TransformGestures_Screen(modifier: Modifier = Modifier) {
5361 Title_Sub_Text (title = " 1. 通过pointInput的detectTransformGestures操作符,Transform操作可执行平移、缩放、旋转,边界约束的等。" )
5462 UI_TransformGestures ()
5563 Title_Desc_Text (desc = " 也可以通过onGesture的内部参数,根据需要,来限定平移边界,缩放边界等各类业务操作。" )
64+ Title_Desc_Text (
65+ desc = " pointInput内还有一些其他感知交互手势的操作函数,以及不同交互方式还有内部的子函数。也可以多个pointInput不同key组合,多重手势交互。" +
66+ " 可根据业务自行定义,比如detectTapGestures,awaitEachGesture,awaitFirstDown等。可查看源码学习实现方式。"
67+ )
68+
69+ Title_Sub_Text (title = " 2.通过draggable操作符实现拖动操作。" )
70+
71+ var orientation by remember { mutableStateOf(Orientation .Horizontal ) }
72+ var offsetX by remember(orientation) { mutableFloatStateOf(0f ) }
73+ var offsetY by remember(orientation) { mutableFloatStateOf(0f ) }
74+ val state = if (orientation == Orientation .Horizontal ) {
75+ rememberDraggableState { delta ->
76+ offsetX + = delta
77+ }
78+ } else {
79+ rememberDraggableState { delta ->
80+ offsetY + = delta
81+ }
82+ }
83+ Title_Desc_Text (desc = " 切换拖拽方向" )
84+ Switch (checked = orientation == Orientation .Horizontal , onCheckedChange = { selected ->
85+ orientation = if (selected) Orientation .Horizontal else Orientation .Vertical
86+ })
87+ // 因为在column中可滚动的,所以有事件冲突。
88+ Box (
89+ modifier = Modifier
90+ .fillMaxWidth()
91+ .height(200 .dp)
92+ ) {
93+ Text (text = " 拽我" , Modifier
94+ // offset实际触发变动
95+ .offset { IntOffset (offsetX.roundToInt(), offsetY.roundToInt()) }
96+ // draggable感知拖拽
97+ .size(50 .dp)
98+ .background(Color .Blue )
99+ .draggable(state, orientation))
100+ }
56101 }
102+
57103}
58104
105+
59106@Composable
60107private fun UI_TransformGestures () {
61108 // 数字小数点的格式化
0 commit comments