|  | 
|  | 1 | +import { useEffect, useRef, useState } from 'react'; | 
|  | 2 | +import { Animated, StyleSheet, View } from 'react-native'; | 
|  | 3 | +import { | 
|  | 4 | +  AnimatedConverter, | 
|  | 5 | +  AnimatedObserver, | 
|  | 6 | +} from 'react-native-animated-observer'; | 
|  | 7 | +import { Gesture, GestureDetector } from 'react-native-gesture-handler'; | 
|  | 8 | +import Reanimated, { | 
|  | 9 | +  useAnimatedStyle, | 
|  | 10 | +  useSharedValue, | 
|  | 11 | +} from 'react-native-reanimated'; | 
|  | 12 | + | 
|  | 13 | +export function AnimatedToReanimated() { | 
|  | 14 | +  const positionAnimated = useState(() => new Animated.Value(0))[0]; | 
|  | 15 | +  const positionReanimated = useSharedValue(0); | 
|  | 16 | + | 
|  | 17 | +  const animatedStyle = useAnimatedStyle(() => { | 
|  | 18 | +    return { | 
|  | 19 | +      transform: [{ translateY: positionReanimated.get() }], | 
|  | 20 | +    }; | 
|  | 21 | +  }); | 
|  | 22 | + | 
|  | 23 | +  return ( | 
|  | 24 | +    <View style={styles.container}> | 
|  | 25 | +      <Animated.ScrollView | 
|  | 26 | +        style={styles.container} | 
|  | 27 | +        contentContainerStyle={styles.content} | 
|  | 28 | +        onScroll={Animated.event( | 
|  | 29 | +          [{ nativeEvent: { contentOffset: { y: positionAnimated } } }], | 
|  | 30 | +          { useNativeDriver: true } | 
|  | 31 | +        )} | 
|  | 32 | +      > | 
|  | 33 | +        {Array.from({ length: 100 }).map((_, index) => ( | 
|  | 34 | +          <View key={index} style={[styles.line]} /> | 
|  | 35 | +        ))} | 
|  | 36 | +      </Animated.ScrollView> | 
|  | 37 | +      <Reanimated.View style={[styles.box, styles.reanimated, animatedStyle]} /> | 
|  | 38 | +      <AnimatedConverter from={positionAnimated} to={positionReanimated} /> | 
|  | 39 | +    </View> | 
|  | 40 | +  ); | 
|  | 41 | +} | 
|  | 42 | + | 
|  | 43 | +export function ReanimatedToAnimated() { | 
|  | 44 | +  const positionAnimated = useState(() => new Animated.Value(0))[0]; | 
|  | 45 | +  const positionReanimated = useSharedValue(0); | 
|  | 46 | + | 
|  | 47 | +  const pan = Gesture.Pan().onUpdate((e) => { | 
|  | 48 | +    positionReanimated.set(e.absoluteX); | 
|  | 49 | +  }); | 
|  | 50 | + | 
|  | 51 | +  const animatedStyle = useAnimatedStyle(() => { | 
|  | 52 | +    return { | 
|  | 53 | +      transform: [{ translateX: positionReanimated.get() }], | 
|  | 54 | +    }; | 
|  | 55 | +  }); | 
|  | 56 | + | 
|  | 57 | +  return ( | 
|  | 58 | +    <GestureDetector gesture={pan}> | 
|  | 59 | +      <View style={styles.container}> | 
|  | 60 | +        <Reanimated.View | 
|  | 61 | +          style={[styles.box, styles.reanimated, animatedStyle]} | 
|  | 62 | +        /> | 
|  | 63 | +        <Animated.View | 
|  | 64 | +          style={[ | 
|  | 65 | +            styles.box, | 
|  | 66 | +            styles.animated, | 
|  | 67 | +            { | 
|  | 68 | +              transform: [{ translateX: positionAnimated }], | 
|  | 69 | +            }, | 
|  | 70 | +          ]} | 
|  | 71 | +        /> | 
|  | 72 | +        <AnimatedConverter from={positionReanimated} to={positionAnimated} /> | 
|  | 73 | +      </View> | 
|  | 74 | +    </GestureDetector> | 
|  | 75 | +  ); | 
|  | 76 | +} | 
|  | 77 | + | 
|  | 78 | +export function PlainValueObserver() { | 
|  | 79 | +  const [value, setValue] = useState(0); | 
|  | 80 | + | 
|  | 81 | +  useEffect(() => { | 
|  | 82 | +    const interval = setInterval(() => { | 
|  | 83 | +      setValue((prev) => prev + 1); | 
|  | 84 | +    }, 3000); | 
|  | 85 | + | 
|  | 86 | +    return () => clearInterval(interval); | 
|  | 87 | +  }, []); | 
|  | 88 | + | 
|  | 89 | +  return ( | 
|  | 90 | +    <AnimatedObserver | 
|  | 91 | +      value={value} | 
|  | 92 | +      onValueChange={(e) => { | 
|  | 93 | +        console.log('PlainObserver onValueChange', e.nativeEvent.value); | 
|  | 94 | +      }} | 
|  | 95 | +    /> | 
|  | 96 | +  ); | 
|  | 97 | +} | 
|  | 98 | + | 
|  | 99 | +export function AnimatedValueObserver() { | 
|  | 100 | +  const ref = useRef(0); | 
|  | 101 | +  const value = useState(() => new Animated.Value(ref.current))[0]; | 
|  | 102 | + | 
|  | 103 | +  useEffect(() => { | 
|  | 104 | +    const interval = setInterval(() => { | 
|  | 105 | +      value.setValue(++ref.current); | 
|  | 106 | +    }, 3000); | 
|  | 107 | + | 
|  | 108 | +    return () => clearInterval(interval); | 
|  | 109 | +  }, [value]); | 
|  | 110 | + | 
|  | 111 | +  return ( | 
|  | 112 | +    <AnimatedObserver | 
|  | 113 | +      value={value} | 
|  | 114 | +      onValueChange={(e) => { | 
|  | 115 | +        console.log('AnimatedObserver onValueChange', e.nativeEvent.value); | 
|  | 116 | +      }} | 
|  | 117 | +    /> | 
|  | 118 | +  ); | 
|  | 119 | +} | 
|  | 120 | + | 
|  | 121 | +export function ReanimatedValueObserver() { | 
|  | 122 | +  const value = useSharedValue(0); | 
|  | 123 | + | 
|  | 124 | +  useEffect(() => { | 
|  | 125 | +    const interval = setInterval(() => { | 
|  | 126 | +      value.set(value.get() + 1); | 
|  | 127 | +    }, 3000); | 
|  | 128 | + | 
|  | 129 | +    return () => clearInterval(interval); | 
|  | 130 | +  }, [value]); | 
|  | 131 | + | 
|  | 132 | +  return ( | 
|  | 133 | +    <AnimatedObserver | 
|  | 134 | +      value={value} | 
|  | 135 | +      onValueChange={(e) => { | 
|  | 136 | +        console.log('ReanimatedObserver onValueChange', e.nativeEvent.value); | 
|  | 137 | +      }} | 
|  | 138 | +    /> | 
|  | 139 | +  ); | 
|  | 140 | +} | 
|  | 141 | + | 
|  | 142 | +const styles = StyleSheet.create({ | 
|  | 143 | +  container: { | 
|  | 144 | +    flex: 1, | 
|  | 145 | +  }, | 
|  | 146 | +  box: { | 
|  | 147 | +    position: 'absolute', | 
|  | 148 | +    top: 10, | 
|  | 149 | +    left: 10, | 
|  | 150 | +    width: 50, | 
|  | 151 | +    height: 50, | 
|  | 152 | +    borderRadius: 5, | 
|  | 153 | +  }, | 
|  | 154 | +  content: { | 
|  | 155 | +    gap: 10, | 
|  | 156 | +    padding: 10, | 
|  | 157 | +  }, | 
|  | 158 | +  line: { | 
|  | 159 | +    height: 10, | 
|  | 160 | +    borderRadius: 5, | 
|  | 161 | +    backgroundColor: 'rgba(0, 0, 0, 0.1)', | 
|  | 162 | +  }, | 
|  | 163 | +  reanimated: { | 
|  | 164 | +    backgroundColor: 'tomato', | 
|  | 165 | +  }, | 
|  | 166 | +  animated: { | 
|  | 167 | +    top: 10, | 
|  | 168 | +    left: 70, | 
|  | 169 | +    backgroundColor: 'blue', | 
|  | 170 | +  }, | 
|  | 171 | +}); | 
0 commit comments