Kurs React Native · Moduł 10: Animacje

react-native-reanimated - Zaawansowane animacje

1 min czytania ·
W tej lekcji4

Reanimated to potężniejsza biblioteka animacji!

Instalacja

npx expo install react-native-reanimated

Konfiguracja babel.config.js

module.exports = {
  presets: ['babel-preset-expo'],
  plugins: ['react-native-reanimated/plugin'], // musi być ostatni!
};

Podstawy Reanimated

import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withSpring,
  withTiming,
} from 'react-native-reanimated';

function SpringBox() {
  const offset = useSharedValue(0);

  const animatedStyles = useAnimatedStyle(() => ({
    transform: [{ translateX: offset.value }],
  }));

  const moveRight = () => {
    offset.value = withSpring(200);
  };

  const moveLeft = () => {
    offset.value = withSpring(0);
  };

  return (
    <View>
      <Animated.View style={[styles.box, animatedStyles]} />
      <Pressable onPress={moveRight}><Text>Prawo</Text></Pressable>
      <Pressable onPress={moveLeft}><Text>Lewo</Text></Pressable>
    </View>
  );
}

Gesty z Gesture Handler

import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import Animated, { useSharedValue, useAnimatedStyle, withSpring } from 'react-native-reanimated';

function DraggableBox() {
  const translateX = useSharedValue(0);
  const translateY = useSharedValue(0);

  const panGesture = Gesture.Pan()
    .onUpdate((event) => {
      translateX.value = event.translationX;
      translateY.value = event.translationY;
    })
    .onEnd(() => {
      translateX.value = withSpring(0);
      translateY.value = withSpring(0);
    });

  const animatedStyle = useAnimatedStyle(() => ({
    transform: [
      { translateX: translateX.value },
      { translateY: translateY.value },
    ],
  }));

  return (
    <GestureDetector gesture={panGesture}>
      <Animated.View style={[styles.box, animatedStyle]} />
    </GestureDetector>
  );
}
Kod do tej lekcji: App.tsx
import React from 'react';
import { View, Text, Pressable, StyleSheet } from 'react-native';
import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withSpring,
} from 'react-native-reanimated';

export default function App() {
  // TODO: utwórz wartość współdzieloną dla przesunięcia w poziomie (start: 0)
  //       const offset = useSharedValue(0);

  // TODO: zbuduj animowany styl, który przesuwa widok o offset.value:
  //       const animatedStyles = useAnimatedStyle(() => ({
  //         transform: [{ translateX: offset.value }],
  //       }));

  const moveRight = () => {
    // TODO: przypisz offset.value = withSpring(120);

  };

  const moveLeft = () => {
    // TODO: przypisz offset.value = withSpring(0);

  };

  return (
    <View style={styles.container}>
      {/* TODO: dołącz animatedStyles do stylu pudełka:
          style={[styles.box, animatedStyles]} */}
      <Animated.View style={styles.box} />

      <View style={styles.row}>
        <Pressable style={styles.button} onPress={moveLeft}>
          <Text style={styles.buttonText}>Lewo</Text>
        </Pressable>
        <Pressable style={styles.button} onPress={moveRight}>
          <Text style={styles.buttonText}>Prawo</Text>
        </Pressable>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#1a5276',
  },
  box: {
    width: 90,
    height: 90,
    borderRadius: 12,
    backgroundColor: '#00d4ff',
    marginBottom: 32,
  },
  row: {
    flexDirection: 'row',
    gap: 12,
  },
  button: {
    paddingVertical: 12,
    paddingHorizontal: 24,
    borderRadius: 8,
    backgroundColor: '#2980b9',
  },
  buttonText: {
    color: '#fff',
    fontSize: 16,
    fontWeight: '600',
  },
});

Hasła z dokumentacji w tej lekcji

Widzisz błąd w tej lekcji?

Sprawdź się

Odpowiedz na pytania z tej lekcji. Wybierz odpowiedź, a od razu zobaczysz, czy jest poprawna.

  1. 1. Jak zainstalować react-native-reanimated?

  2. 2. Gdzie musi być plugin reanimated w babel.config.js?

To 2 z 9 pytań do tej lekcji. Pozostałe rozwiążesz w grze.

Zadania praktyczne w grze

  • Układanie w poziomie

    Ułóż useAnimatedStyle:

  • Klikanie w kolejności

    Kliknij elementy withTiming:

  • Edytor kodu

    Uzupełnij kod z withSpring.

  1. PytanieInstalacja Reanimated
  2. Pytaniebabel.config.js
  3. PytanieuseSharedValue
  4. PytanieModyfikacja .value
  5. Ułóż w kolejnościuseAnimatedStyle
  6. PytaniewithSpring
  7. Kliknij w kolejnościwithTiming config
  8. Zadanie z kodemSpring animation
  9. PytanieGesture.Pan
  10. PytanieGestureDetector
  11. PytanieonUpdate
  12. PytanierunOnJS

Przydatne artykuły