I’m a beginner to React Native (Expo Go) and I am trying to make the following animation:
On app launch, an image starts at a random position around halfway up the screen and moves upwards until it gets out of bounds.
Once out of bounds, the image now comes up from the bottom of the screen.
What I’ve had trouble with is having the image start midway on ONLY the first animation.
I’ve played around with different Reanimated libraries such as withRepeat and withTiming with no avail.
2
Answers
Try like this
Snack Link: https://snack.expo.dev/@cyberking40/7d4562
Doing this in reanimated is a kind of tricky. You can use
withRepeat
andwithSequence
to get smooth looping animations; but only once the you’ve completed the first scroll up screen. And as of getting the image to start at random place in the middle of the screen you can useuseSharedValue
along withuseWindowDimensions
to get the image position near the halfway mark:Now we set up the animated style using
width
andheight
to prevent the position from traveling offscreen:And then because you may want to use a similar looping animation on the the x value I decided to create a function for looping (
distRatio
is used to fine tune the animation duration):And bringing it all together (demo):