在Jetpack Compose中,当文本被点击时,我需要开始旋转文本.协程运行,当它完成时,动画应该停止.
到目前为止,我有:
var isRotating by remember { mutableStateOf(false) }
val angle = animateFloatAsState(
targetValue = if (isRotating) 360f else 0f,
animationSpec = infiniteRepeatable(tween(2000, easing = LinearEasing))
)
Box(
modifier = Modifier
.graphicsLayer { rotationZ = angle.value }
.clickable {
isRotating = true
coroutineScope.launch {
doSlowProcess()
isRotating = false
}
}
) {
Text("START")
}
文本"Start"应该旋转,直到"doSlowProcess"运行.它开始旋转,但当它结束时,它并没有停止,只是以另一种方式开始旋转,速度更慢,当达到0°时,它翻转180度,然后继续.如果我再次单击它,它会进行正确的旋转,但在0°时会向前 skip 90°.我猜,如果当"isRotating"变为假时停止旋转,这些"异常"就会消失.我应该如何正确地开始和停止轮换?