-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Terry Sahaidak <tasssik@gmail.com> Co-authored-by: William Candillon <wcandillon@gmail.com> Co-authored-by: Victor Malov <victor.malov@breeffy.com>
- Loading branch information
1 parent
f020668
commit aa0b739
Showing
7 changed files
with
844 additions
and
271 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,330 @@ | ||
/* eslint-disable */ | ||
import React from 'react'; | ||
import { StyleSheet, Button, View } from 'react-native'; | ||
import { PanGestureHandler } from 'react-native-gesture-handler'; | ||
import Animated, { | ||
useSharedValue, | ||
useDerivedValue, | ||
useAnimatedStyle, | ||
useAnimatedScrollHandler, | ||
useAnimatedGestureHandler, | ||
Easing, | ||
withTiming, | ||
withSpring, | ||
cancelAnimation, | ||
delay, | ||
repeat, | ||
sequence, | ||
withDecay, | ||
} from 'react-native-reanimated'; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
}, | ||
box: { | ||
height: 50, | ||
backgroundColor: 'blue', | ||
}, | ||
}); | ||
|
||
/** | ||
* Reanimated 1 | ||
*/ | ||
|
||
// @TODO: add reanimated 1 tests here | ||
|
||
/** | ||
* Reanimated 2 Hooks | ||
*/ | ||
|
||
// useSharedValue | ||
function SharedValueTest() { | ||
const translate = useSharedValue(0); | ||
return <Animated.View style={styles.container} />; | ||
} | ||
|
||
// useAnimatedStyle | ||
function AnimatedStyleTest() { | ||
const width = useSharedValue(50); | ||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
width: width.value, | ||
}; | ||
}); | ||
return <Animated.View style={[styles.box, animatedStyle]} />; | ||
} | ||
|
||
// useDerivedValue | ||
function DerivedValueTest() { | ||
const progress = useSharedValue(0); | ||
const width = useDerivedValue(() => { | ||
return progress.value * 250; | ||
}); | ||
|
||
return ( | ||
<Button title="Random" onPress={() => (progress.value = Math.random())} /> | ||
); | ||
} | ||
|
||
// useAnimatedScrollHandler | ||
function AnimatedScrollHandlerTest() { | ||
const translationY = useSharedValue(0); | ||
const scrollHandler = useAnimatedScrollHandler((event) => { | ||
translationY.value = event.contentOffset.y; | ||
}); | ||
const stylez = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateY: translationY.value, | ||
}, | ||
], | ||
}; | ||
}); | ||
return ( | ||
<View style={styles.container}> | ||
<Animated.View style={[styles.box, stylez]} /> | ||
<Animated.ScrollView onScroll={scrollHandler} scrollEventThrottle={16} /> | ||
</View> | ||
); | ||
} | ||
|
||
// useAnimatedGestureHandler | ||
function AnimatedGestureHandlerTest() { | ||
const x = useSharedValue(0); | ||
const gestureHandler = useAnimatedGestureHandler({ | ||
onStart: (_, ctx) => { | ||
ctx.startX = x.value; | ||
}, | ||
onActive: (event, ctx) => { | ||
x.value = ctx.startX + event.translationX; | ||
}, | ||
onEnd: (_) => { | ||
x.value = 0; | ||
}, | ||
}); | ||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateX: x.value, | ||
}, | ||
], | ||
}; | ||
}); | ||
return ( | ||
<PanGestureHandler onGestureEvent={gestureHandler}> | ||
<Animated.View style={[styles.box, animatedStyle]} /> | ||
</PanGestureHandler> | ||
); | ||
} | ||
|
||
/** | ||
* Reanimated 2 Animations | ||
*/ | ||
|
||
// withTiming | ||
function WithTimingTest() { | ||
const width = useSharedValue(50); | ||
const style = useAnimatedStyle(() => { | ||
return { | ||
width: withTiming(width.value, { | ||
duration: 500, | ||
easing: Easing.bezier(0.25, 0.1, 0.25, 1), | ||
}), | ||
}; | ||
}); | ||
return ( | ||
<View> | ||
<Animated.View style={[styles.box, style]} /> | ||
<Button onPress={() => (width.value = Math.random() * 300)} title="Hey" /> | ||
</View> | ||
); | ||
} | ||
|
||
// withSpring | ||
function WithSpringTest() { | ||
const x = useSharedValue(0); | ||
const gestureHandler = useAnimatedGestureHandler({ | ||
onStart: (_, ctx) => { | ||
ctx.startX = x.value; | ||
}, | ||
onActive: (event, ctx) => { | ||
x.value = ctx.startX + event.translationX; | ||
}, | ||
onEnd: (_) => { | ||
x.value = withSpring(0); | ||
}, | ||
}); | ||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateX: x.value, | ||
}, | ||
], | ||
}; | ||
}); | ||
return ( | ||
<PanGestureHandler onGestureEvent={gestureHandler}> | ||
<Animated.View style={[styles.box, animatedStyle]} /> | ||
</PanGestureHandler> | ||
); | ||
} | ||
|
||
// cancelAnimation | ||
function CancelAnimationTest() { | ||
const x = useSharedValue(0); | ||
const gestureHandler = useAnimatedGestureHandler({ | ||
onStart: (_, ctx) => { | ||
cancelAnimation(x); | ||
}, | ||
onActive: (event, ctx) => { | ||
x.value = ctx.startX + event.translationX; | ||
}, | ||
onEnd: (_) => { | ||
x.value = 0; | ||
}, | ||
}); | ||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateX: x.value, | ||
}, | ||
], | ||
}; | ||
}); | ||
return ( | ||
<PanGestureHandler onGestureEvent={gestureHandler}> | ||
<Animated.View style={[styles.box, animatedStyle]} /> | ||
</PanGestureHandler> | ||
); | ||
} | ||
|
||
// delay | ||
function DelayTest() { | ||
const x = useSharedValue(0); | ||
const gestureHandler = useAnimatedGestureHandler({ | ||
onStart: (_, ctx) => { | ||
cancelAnimation(x); | ||
}, | ||
onActive: (event, ctx) => { | ||
x.value = ctx.startX + event.translationX; | ||
}, | ||
onEnd: (_) => { | ||
x.value = delay(1000, withTiming(70)); | ||
}, | ||
}); | ||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateX: x.value, | ||
}, | ||
], | ||
}; | ||
}); | ||
return ( | ||
<PanGestureHandler onGestureEvent={gestureHandler}> | ||
<Animated.View style={[styles.box, animatedStyle]} /> | ||
</PanGestureHandler> | ||
); | ||
} | ||
|
||
// repeat | ||
function RepeatTest() { | ||
const x = useSharedValue(0); | ||
const gestureHandler = useAnimatedGestureHandler({ | ||
onStart: (_, ctx) => { | ||
cancelAnimation(x); | ||
}, | ||
onActive: (event, ctx) => { | ||
x.value = ctx.startX + event.translationX; | ||
}, | ||
onEnd: (_) => { | ||
x.value = repeat(withTiming(70), 1, true); | ||
}, | ||
}); | ||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateX: x.value, | ||
}, | ||
], | ||
}; | ||
}); | ||
return ( | ||
<PanGestureHandler onGestureEvent={gestureHandler}> | ||
<Animated.View style={[styles.box, animatedStyle]} /> | ||
</PanGestureHandler> | ||
); | ||
} | ||
|
||
// sequence | ||
function SequenceTest() { | ||
const x = useSharedValue(0); | ||
const gestureHandler = useAnimatedGestureHandler({ | ||
onStart: (_, ctx) => { | ||
cancelAnimation(x); | ||
}, | ||
onActive: (event, ctx) => { | ||
x.value = ctx.startX + event.translationX; | ||
}, | ||
onEnd: (_) => { | ||
x.value = sequence(withTiming(70), withTiming(70)); | ||
}, | ||
}); | ||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateX: x.value, | ||
}, | ||
], | ||
}; | ||
}); | ||
return ( | ||
<PanGestureHandler onGestureEvent={gestureHandler}> | ||
<Animated.View style={[styles.box, animatedStyle]} /> | ||
</PanGestureHandler> | ||
); | ||
} | ||
|
||
// withDecay | ||
function WithDecayTest() { | ||
const x = useSharedValue(0); | ||
|
||
const gestureHandler = useAnimatedGestureHandler({ | ||
onStart: (_, ctx) => { | ||
ctx.startX = x.value; | ||
}, | ||
onActive: (event, ctx) => { | ||
x.value = ctx.startX + event.translationX; | ||
}, | ||
onEnd: (evt) => { | ||
x.value = withDecay({ | ||
velocity: evt.velocityX, | ||
clamp: [0, 200], // optionally define boundaries for the animation | ||
}); | ||
}, | ||
}); | ||
|
||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateX: x.value, | ||
}, | ||
], | ||
}; | ||
}); | ||
|
||
return ( | ||
<PanGestureHandler onGestureEvent={gestureHandler}> | ||
<Animated.View style={[styles.box, animatedStyle]} /> | ||
</PanGestureHandler> | ||
); | ||
} |
Oops, something went wrong.