https://github.com/lottie-react-native/lottie-react-native

Animation

在PanResponder中使用动画事件的示例

  1. import React, { useRef } from "react";
  2. import { Animated, View, StyleSheet, PanResponder, Text } from "react-native";
  3. const App = () => {
  4. const pan = useRef(new Animated.ValueXY()).current;
  5. const panResponder = useRef(
  6. PanResponder.create({
  7. onMoveShouldSetPanResponder: () => true,
  8. onPanResponderMove: Animated.event([
  9. null,
  10. { dx: pan.x, dy: pan.y }
  11. ]),
  12. onPanResponderRelease: () => {
  13. Animated.spring(pan, { toValue: { x: 0, y: 0 } }).start();
  14. }
  15. })
  16. ).current;
  17. return (
  18. <View style={styles.container}>
  19. <Text style={styles.titleText}>Drag & Release this box!</Text>
  20. <Animated.View
  21. style={{
  22. transform: [{ translateX: pan.x }, { translateY: pan.y }]
  23. }}
  24. {...panResponder.panHandlers}
  25. >
  26. <View style={styles.box} />
  27. </Animated.View>
  28. </View>
  29. );
  30. }
  31. const styles = StyleSheet.create({
  32. container: {
  33. flex: 1,
  34. alignItems: "center",
  35. justifyContent: "center"
  36. },
  37. titleText: {
  38. fontSize: 14,
  39. lineHeight: 24,
  40. fontWeight: "bold"
  41. },
  42. box: {
  43. height: 150,
  44. width: 150,
  45. backgroundColor: "blue",
  46. borderRadius: 5
  47. }
  48. });
  49. export default App;