Skip to content

Instantly share code, notes, and snippets.

@cp-radhika-s
Created October 5, 2022 11:40
Show Gist options
  • Save cp-radhika-s/5bbb2e3cf8700a804f9fca964909fbc8 to your computer and use it in GitHub Desktop.
Save cp-radhika-s/5bbb2e3cf8700a804f9fca964909fbc8 to your computer and use it in GitHub Desktop.
fun Modifier.shakeClickEffect() = composed {
var buttonState by remember { mutableStateOf(ButtonState.Idle) }
val tx by animateFloatAsState(
targetValue = if (buttonState == ButtonState.Pressed) 0f else -50f,
animationSpec = repeatable(
iterations = 2,
animation = tween(durationMillis = 50, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
)
)
this
.graphicsLayer {
translationX = tx
}
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null,
onClick = { }
)
.pointerInput(buttonState) {
awaitPointerEventScope {
buttonState = if (buttonState == ButtonState.Pressed) {
waitForUpOrCancellation()
ButtonState.Idle
} else {
awaitFirstDown(false)
ButtonState.Pressed
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment