Created
April 23, 2021 23:12
-
-
Save pauloaapereira/cf7f2fef77bfeaa86f8effc4611569d2 to your computer and use it in GitHub Desktop.
Jetpack Compose - Earthquake Effect_9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun EarthquakeBox( | |
onEarthquakeFinished: () -> Unit = {}, | |
content: @Composable IEarthquakeScope.() -> Unit | |
) { | |
val coroutineScope = rememberCoroutineScope() | |
val state = remember { EarthquakeState() } | |
val scope = remember { EarthquakeScope(state = state) } | |
val mover = remember { EarthquakeMover() } | |
val controller = remember { | |
EarthquakeController( | |
scope = coroutineScope, | |
mover = mover, | |
onEarthquakeFinished = { | |
state.isShaking = false | |
onEarthquakeFinished() | |
} | |
) | |
} | |
LaunchedEffect(state.isShaking) { | |
if (state.isShaking) { | |
controller.startShaking( | |
earthquakeDuration = state.earthquakeDuration, | |
shakeDuration = 1000L / state.shakesPerSecond, | |
shakeForce = state.shakeForce | |
) | |
} else { | |
controller.stopShaking() | |
} | |
} | |
Box( | |
modifier = Modifier | |
.alpha(mover.alpha.value) | |
.offset(mover.x.value.dp, mover.y.value.dp) | |
.rotate(mover.rotation.value) | |
.padding(state.shakeForce.dp) | |
) { | |
scope.content() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment