android - Jetpack Compose 中的长按手势
解决方案
您可以使用 aTransition
来管理按下和释放状态之间的动画。
enum class ComponentState { Pressed, Released }
var toState by remember { mutableStateOf(ComponentState.Released) }
val transition: Transition<ComponentState> = updateTransition(targetState = toState, label = "")
// Defines a float animation to scale x,y
val scalex: Float by transition.animateFloat(
transitionSpec = { spring(stiffness = 50f) }, label = ""
) { state ->
if (state == ComponentState.Pressed) 0.90f else 1f
}
val scaley: Float by transition.animateFloat(
transitionSpec = { spring(stiffness = 50f) }, label = ""
) { state ->
if (state == ComponentState.Pressed) 0.90f else 1f
}
然后您可以使用PointerInputScope.detectTapGestures
来检测按下手势:
val modifier = Modifier.pointerInput(Unit) {
detectTapGestures(
onPress = {
toState = ComponentState.Pressed
tryAwaitRelease()
toState = ComponentState.Released
}
)
}
最后将动画应用到您的 Composable。
例如:
Box(
modifier
.width((100 * scalex).dp)
.height((100 * scaley).dp),
contentAlignment = Alignment.Center) {
Image(
//...
modifier = Modifier.graphicsLayer{
scaleX = scalex;
scaleY = scaley
})
}
推荐阅读
- git - 在 Ubuntu 上使用 eCryptfs 加密 /home 后 Git “文件名太长”
- asp.net-core - ORA-00955 错误
- python - 在一个大应用程序中捆绑多个独立但相关的 Python 工具
- vb.net - 后台取消事件表单关闭错误
- javascript - 哪种数据结构最适合这项任务?
- string - MIPS 运行时异常和地址超出范围
- bash - 使用 find 和 xargs 时从 ffmpeg 重命名输出文件名
- gatsby - 在路线之间导航时如何防止 gatsby 布局重新渲染?
- ruby-on-rails - 为什么 simple_form 使用嵌套资源引发“无路由匹配 [POST]“/tenants””错误
- android - 当我单击任何按钮时,应用程序将崩溃