技术探索:开源分享 - 在Jetpack Compose中实现iOS丝滑左滑菜单交互设计

背景

最近遇到个需求,jetpack compose中要给列表的item加一个左滑删除菜单,左滑展示一个删除按钮,点击删除item,很简单的需求,然而Google了一圈,只发现了compose自带的SwipeToDismiss和类似的一个开源库me.saket.swipe:swipe比较有用,但是细看不太符合需求,SwipeToDismiss顾名思义就是滑动到某个地方item就会dismiss,不符合要求的swipe后点击再dismiss的需求啊(测试不接受你有啥办法),自己动手呗!!

swipe这个库写得比较简单且支持compose multiplatform,果断在这个库的基础上来修改。

先上成果

少啰嗦,先看东西!觉得有用的话给个star呗~

swipe-like-ios

swipe-like-ios builds composables that can be swiped left or right for revealing actions

groovy 复制代码
// settings.gradle.kts
repositories {
  maven { setUrl("https://www.jitpack.io") }
}

// build.gradle.kts
implementation("com.linversion.swipe:swipe-like-ios:latest-version")
kotlin 复制代码
val archive = SwipeAction(
  icon = rememberVectorPainter(Icons.TwoTone.Archive),
  background = Color.Green,
  onClick = { ... }
)

val replyAll = SwipeAction(
  icon = rememberVectorPainter(Icons.TwoTone.ReplyAll),
  background = Color.Perfume,
  onClick = { println("Reply swiped") }
)
  
val snooze = SwipeAction(
  icon = { Text("Snooze") },
  background = Color.Yellow,
  onClick = { ... },
)

SwipeableActionsBox(
  startActions = listOf(replyAll),
  endActions = listOf(snooze, archive),
  swipeThreshold = 80.dp,
) {
  // Swipeable content goes here.
}

简单介绍一下实现原理

其实很简单,就是在item的左右两边分别用一个Row放置你的菜单(action),给个初始的offsetX,使得菜单刚好不可见,滑动的时候改变ofssetX,每个action平分滑动的空间,直到菜单完全展示后加一个阻尼的效果,完全仿照iOS的实现。

对比之下SwipeToDismiss的原理是在item的底部放一个Row来摆放你的action,滑动的时候就可以看到了。

滑动的监听则都是用Modifier的draggable修饰符,然后给item设置absoluteOffset

kotlin 复制代码
Box(
    modifier = Modifier
      .absoluteOffset { IntOffset(x = offsetX, y = 0) }
      .draggable(
        orientation = Horizontal,
        enabled = !state.isResettingOnRelease,
        onDragStopped = {
          scope.launch {
            state.handleOnDragStopped()
          }
        },
        state = state.draggableState,
      ),
    content = content
  )

后续期望

仔细看了一下,iOS在有些场景也有SwipeToDismiss的效果,并且加了震动效果,后续会考虑拓展加上这个效果。

相关推荐
晓梦林10 分钟前
[Dest0g3 520迎新赛]EasyPHP-学习笔记
android·笔记·学习
朱涛的自习室37 分钟前
Munk AI 桌面端「预告」
android·前端·人工智能
程序员正茂1 小时前
Android studio中使用OpenCV C++库
android·c++·opencv·mobile
逛逛GitHub1 小时前
很多神级开源 Agent 从它演化而来:77K Star 的 Pi 太强了。
github
恋猫de小郭1 小时前
Android R8 为什么可以让 Kotlin 协程提速 2 倍?
android·前端·flutter
李斯维1 小时前
Jetpack UI状态模型 ViewModel 的入门和使用
android·android studio·android jetpack
2501_915909061 小时前
iOS应用性能监控 Instruments工具与崩溃日志分析完整指南
android·ios·小程序·https·uni-app·iphone·webview
杉氧1 小时前
弹性滚动的奥秘:在 Flutter 中使用 CustomScrollView 与 Sliver 打造极致流畅列表
android·前端·flutter
黄林晴2 小时前
你敢信吗?R8让 Compose 协程直接快 2 倍!你敢相信吗?R8 让合成程度直接协调快速 2 倍!
android
今夕资源网2 小时前
SayIt语音输入+AI 润色 Typeless 替代品 GitHub开源语音输入法,可本地部署LLM亦可接入deepseek 或者其他大模型。
人工智能·开源·github·输入法·开源输入法·语音输入法