我正在开发一个使用动画角色的应用程序,当用户点击它时,该角色会触发不同的动画,当用户滑动它时,该角色会触发另一个动画.问题是视图将滑动手势注册为点击,如下所示.

enter image description here

以下是处理指针输入的代码:

import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.tooling.preview.Preview
import com.airbnb.lottie.compose.*
import com.lumen.tomo.R
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun ChatView() {
    val initialAnimation = R.raw.cat_idle  // Initial animation resource ID
    val tapAnimation = R.raw.cat_headshake          // Tap specific animation resource ID
    val swipeAnimation = R.raw.cat_heart      // Swipe specific animation resource ID

    var animationRes by remember { mutableIntStateOf(initialAnimation) }
    val idleAnimationComposition by rememberLottieComposition(LottieCompositionSpec.RawRes(initialAnimation))
    val tapAnimationComposition by rememberLottieComposition(LottieCompositionSpec.RawRes(tapAnimation))
    val swipeAnimationComposition by rememberLottieComposition(LottieCompositionSpec.RawRes(swipeAnimation))
    val coroutineScope = rememberCoroutineScope()  // Remember a CoroutineScope tied to the Composable's lifecycle

    LottieAnimation(
        composition = if (animationRes == initialAnimation) idleAnimationComposition else if (animationRes == tapAnimation) tapAnimationComposition else swipeAnimationComposition,  // Choose based on animationRe
        iterations = LottieConstants.IterateForever,
        modifier = Modifier
            .fillMaxSize()
            .pointerInput(Unit) {
                detectTapGestures(onTap = {
                    coroutineScope.launch {
                        animationRes = tapAnimation
                        delay(5000)
                        animationRes = initialAnimation
                    }
                })
                detectDragGestures { _, _ ->
                    coroutineScope.launch {
                        animationRes = swipeAnimation
                        delay(5000)
                        animationRes = initialAnimation
                    }
                }
            }
    )
}

推荐答案

看看official documentation:

在内部,detectTapGestures方法会阻止协程,并且永远无法到达第二个检测器.如果您需要向组合对象添加多个手势监听器,请改用单独的pointerInput修饰符实例.

请更新您的代码并使用两个单独的pointerInput修改器:

modifier = Modifier
    .fillMaxSize()
    .pointerInput(Unit) {
        detectTapGestures(onTap = {
            coroutineScope.launch {
                animationRes = tapAnimation
                delay(5000)
                animationRes = initialAnimation
            }
        })
    }
    .pointerInput(Unit) {
        detectDragGestures { _, _ ->
            coroutineScope.launch {
                animationRes = swipeAnimation
                delay(5000)
                animationRes = initialAnimation
            }
        }
    }

Android相关问答推荐

在编写中强制软键盘呈现

使用Retrofit2的API调用:我如何能够一直进行API调用,以更新数据而无需重新打开应用程序

Google Play测试应用程序Crash-java.lang.NoSuchFieldError:没有Lkotlinx/coroutines/CoroutineExceptionHandler类型的字段键

从安卓S原生库的资源中读取json文件

如何制作安卓';s FileProvider在Android 11上使用外部存储+

如何在使用 PointerInput 修改器时添加点击时的波纹效果

Android kotlin 中闪屏 API 执行完成后如何根据身份验证将用户导航到特定屏幕

Android Studio 在 list 文件中已经声明了活动类,但仍出现无法找到明确的活动类的错误

具有数据库和升级潜力的移动应用程序开发(Android)供朋友使用

Kotlin 协程、 retrofit 、android

Jetpack Compose:如何绘制异形边框?

如何在 Jetpack Compose 中创建无限pager

Visual Studio 无法在 Android 上编译 .NET MAUI 项目

在 Kotlin 中设置 startActivity() 时类型不匹配

您如何衡量条形图的 4 个类别?

记住或不记得derivedStateOf

如何处理 com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: For input string: "T1V 4Y8" Kotlin

Android Studio Emulator Internet 连接问题仅是第一次

CenterAlignedTopAppBar 滚动行为:未为参数状态传递值

升级到 android studio 花栗鼠后,应用程序未安装在模拟器中