Skip to content

Commit 3c08a34

Browse files
author
iOrchid
committed
draggable
1 parent b840531 commit 3c08a34

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

compose/src/main/java/org/zhiwei/compose/screen/gesture/TransformGestures.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ package org.zhiwei.compose.screen.gesture
33
import android.icu.text.DecimalFormat
44
import androidx.compose.foundation.Image
55
import androidx.compose.foundation.background
6+
import androidx.compose.foundation.gestures.Orientation
67
import androidx.compose.foundation.gestures.detectTransformGestures
8+
import androidx.compose.foundation.gestures.draggable
9+
import androidx.compose.foundation.gestures.rememberDraggableState
710
import androidx.compose.foundation.layout.Box
811
import androidx.compose.foundation.layout.Column
912
import androidx.compose.foundation.layout.fillMaxSize
1013
import androidx.compose.foundation.layout.fillMaxWidth
1114
import androidx.compose.foundation.layout.height
15+
import androidx.compose.foundation.layout.offset
1216
import androidx.compose.foundation.layout.padding
17+
import androidx.compose.foundation.layout.size
1318
import androidx.compose.foundation.rememberScrollState
1419
import androidx.compose.foundation.verticalScroll
20+
import androidx.compose.material.Switch
1521
import androidx.compose.material3.Text
1622
import androidx.compose.runtime.Composable
1723
import androidx.compose.runtime.getValue
@@ -31,12 +37,14 @@ import androidx.compose.ui.input.pointer.pointerInput
3137
import androidx.compose.ui.layout.ContentScale
3238
import androidx.compose.ui.res.painterResource
3339
import androidx.compose.ui.tooling.preview.Preview
40+
import androidx.compose.ui.unit.IntOffset
3441
import androidx.compose.ui.unit.dp
3542
import org.zhiwei.compose.R
3643
import org.zhiwei.compose.ui.widget.Title_Desc_Text
3744
import org.zhiwei.compose.ui.widget.Title_Sub_Text
3845
import org.zhiwei.compose.ui.widget.Title_Text
3946
import kotlin.math.cos
47+
import kotlin.math.roundToInt
4048
import 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
60107
private fun UI_TransformGestures() {
61108
//数字小数点的格式化

0 commit comments

Comments
 (0)