Lädt...

🔧 React Native Animation: Building Play/Pause Controls with Lottie in React Native


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In mobile app development, adding animations can greatly enhance the user experience by making the app more engaging and dynamic. React Native provides various libraries and tools to incorporate animations seamlessly into your application. One such popular library is Lottie, which allows you to integrate Adobe After Effects animations into your React Native projects effortlessly.

In this tutorial, we'll learn how to create a simple yet powerful animated button in React Native to start and stop animations using the Lottie library. By the end of this tutorial, you'll have a clear understanding of how to control animations dynamically based on user interactions.

Prerequisites:
Before diving into this tutorial, you should have a basic understanding of React Native and how to set up a development environment. Make sure you have Node.js and npm (or yarn) installed on your machine.

Step1: Setting Up the Project
To learn how to set up visit this link project-setup.

Step2: Install the Lottie library: lottie-react-native.

If you don’t have any lottie animation then download it from the lottie website. Here are some free-lottie-animations you can visit. Note you have to download the json of it.

Step3:
Move your animation json file inside the assets folder. In my case I created a new directory inside the assets name called animation and inside of it I moved my json file.

click here

Step4: Creating the Screen Page and adding button element to run functioning the animation.

// HomeScreen.tsx

import LottieView from "lottie-react-native";
import { Fragment, useRef, useState } from "react";
import { Button, Dimensions, StyleSheet, Text, View } from "react-native";

export default function HomeScreen() {
  const [playPause, setPlayPause] = useState<string>("play");
  const animationRef = useRef<LottieView>(null);

  const runAnimation = (mode: string) => {
    if (mode === "play") {
      animationRef?.current?.play();
    }

    if (mode === "pause") {
      animationRef?.current?.pause();
    }
  };

  return (
    <Fragment>
      <View style={styles.container}>
        <View style={styles.animationBox}>
          <LottieView
            ref={animationRef}
            source={require("../../assets/animation/animation_1.json")}
            autoPlay={false}
            loop={false}
            style={{
              width: Dimensions.get("window").width - 100,
              height: Dimensions.get("window").height - 20,
            }}
          />
        </View>
        <View style={styles.btnBox}>
          <Button onPress={() => runAnimation("play")} title={"Start"} />
          <Button onPress={() => runAnimation("pause")} title={"Pause"} />
        </View>
      </View>
    </Fragment>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
  animationBox: {
    flex: 0.7,
    width: "100%",
    justifyContent: "center",
    alignItems: "center",
  },
  btnBox: {
    flex: 0.3,
    width: "50%",
    flexDirection: "row",
    justifyContent: "space-evenly",
    alignItems: "center",
  },
  btn: {
    width: "50%",
  },
});

Output:

output

Conclusion:
In this guide, we've explored the integration of Lottie animations within React Native applications, focusing specifically on implementing play and pause controls. Leveraging the Lottie library, we've crafted a user-friendly animated button component capable of dynamically initiating and halting animations based on user interaction.

...

🔧 React Native Animation: Building Play/Pause Controls with Lottie in React Native


📈 88.13 Punkte
🔧 Programmierung

🔧 How to use a Lottie animation in your React app


📈 38.75 Punkte
🔧 Programmierung

🔧 Lottie Animation: Flutter


📈 34.65 Punkte
🔧 Programmierung

🔧 Day 22. Lottie animation


📈 34.65 Punkte
🔧 Programmierung

🔧 🚀 Implementing Pull-to-Refresh in React Native with Reanimated and Lottie 🎉


📈 32.68 Punkte
🔧 Programmierung

🔧 Enhancing Your React Native App with Stunning Lottie Animations


📈 32.68 Punkte
🔧 Programmierung

🔧 React Native Lottie Animations


📈 32.68 Punkte
🔧 Programmierung

🔧 Day 4: React Native Onboarding UI using Lottie Animations


📈 32.68 Punkte
🔧 Programmierung

🔧 Building Animated Buttons in Flutter with Lottie


📈 28.42 Punkte
🔧 Programmierung

🔧 Rendering( or How to Render) Animation in JSON format with LottieFiles animation in React application


📈 27.46 Punkte
🔧 Programmierung

🔧 Bind Controls to Other Controls (11 of 18) | Building Apps with XAML and .NET MAUI


📈 26.69 Punkte
🔧 Programmierung

📰 Bash add pause prompt in a shell script ( bash pause command )


📈 25.9 Punkte
🐧 Unix Server

📰 Android: Google ermöglicht Pause von der Pause – Digital Wellbeing lässt sich temporär deaktivieren


📈 25.9 Punkte
📰 IT Nachrichten

📰 Hulu, AT&amp;T To Test 'Pause Ads' In 2019, Automatically Playing Commercials When You Hit Pause


📈 25.9 Punkte
📰 IT Security Nachrichten

🔧 Building a Modern Document Website for React Native Library Like React Native ECharts


📈 24.87 Punkte
🔧 Programmierung

🎥 Use scroll progress to start, pause, and reverse an animation on your web page


📈 24.63 Punkte
🎥 Videos

🔧 Animation -17 : Staggered Stair Loading Animation


📈 23.36 Punkte
🔧 Programmierung

🔧 Animation -13 : Simple Content Preload CSS Animation


📈 23.36 Punkte
🔧 Programmierung

🐧 Pencil2D Animation - Opensource animation software


📈 23.36 Punkte
🐧 Linux Tipps

🔧 Animation -22 : Cube Flipping Loader CSS Animation


📈 23.36 Punkte
🔧 Programmierung

🔧 Animation -19 : Simple preloader CSS Animation


📈 23.36 Punkte
🔧 Programmierung

🔧 Specify how multiple animation effects should composite with `animation-composition`


📈 23.36 Punkte
🔧 Programmierung

🔧 Animation -18 : Pure CSS Gooey Loader Animation


📈 23.36 Punkte
🔧 Programmierung

🔧 Architecture Guide: Building Scalable React (or React Native) Apps with Zustand & React Query


📈 23.35 Punkte
🔧 Programmierung

🕵️ LottieFiles confirmed a supply chain attack on Lottie-Player


📈 22.97 Punkte
🕵️ Hacking

🔧 Lottie Player npm package compromised for crypto wallet theft


📈 22.97 Punkte
🔧 Programmierung

📰 LottieFiles Issues Warning About Compromised "lottie-player" npm Package


📈 22.97 Punkte
📰 IT Security Nachrichten

📰 Lottie Player supply chain compromise: Sites, apps showing crypto scam pop-ups


📈 22.97 Punkte
📰 IT Security Nachrichten

🔧 Json Lottie Animations & Custom Error Pages in Nuxt


📈 22.97 Punkte
🔧 Programmierung

🔧 Creating Custom Lottie Animations With SVGator


📈 22.97 Punkte
🔧 Programmierung

🔧 Solving Memory Issues with Lottie SVG Animations


📈 22.97 Punkte
🔧 Programmierung

📰 WhatsApp will animierte Emojis auf Basis von Lottie einführen


📈 22.97 Punkte
📰 IT Nachrichten

🔧 The Easiest Way to Animate WordPress Websites: BeBuilder + Lottie


📈 22.97 Punkte
🔧 Programmierung

🔧 Getting Started with Lottie.js


📈 22.97 Punkte
🔧 Programmierung

🎥 Bring Stunning Animations to Your Apps with Lottie | Xamarin Developer Summit


📈 22.97 Punkte
🎥 Video | Youtube