From fdcd66efbef9e795b2506f2dd69dc35a1b42cf71 Mon Sep 17 00:00:00 2001 From: DaniAkash Date: Tue, 3 Mar 2020 06:52:04 +0530 Subject: [PATCH 1/7] Animated Fading View --- docs/animated.md | 167 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 159 insertions(+), 8 deletions(-) diff --git a/docs/animated.md b/docs/animated.md index 434efe5984f..121183633e6 100644 --- a/docs/animated.md +++ b/docs/animated.md @@ -5,18 +5,169 @@ title: Animated The `Animated` library is designed to make animations fluid, powerful, and painless to build and maintain. `Animated` focuses on declarative relationships between inputs and outputs, with configurable transforms in between, and `start`/`stop` methods to control time-based animation execution. -The most basic workflow for creating an animation is to create an `Animated.Value`, hook it up to one or more style attributes of an animated component, and then drive updates via animations using `Animated.timing()`: +The most basic workflow for creating an animation is to create an `Animated.Value`, hook it up to one or more style attributes of an animated component, and then drive updates via animations using `Animated.timing()` + +> The animated value should not be modified directly like Javascript variables hence it is usually kept as a state variable in class components. Functional components with hooks can use `useRef` hook which returns a mutable ref object whose `current` property is initialized as the given argument and persists throughout the component lifecycle. + +The following example contains a simple view which will fade in & fade out based on the animated value `fadeAnim` + +
+ +
+ + + +```SnackPlayer name=Animated +import React, { useRef } from "react"; +import { Animated, Text, View, StyleSheet, Button } from "react-native"; + +export default function App() { + // fadeAnim will be used as the value for opacity. Initial Value: 0 + const fadeAnim = useRef(new Animated.Value(0)).current; + + const fadeIn = () => { + // Will change fadeAnim value to 1 in 5 seconds + Animated.timing(fadeAnim, { + toValue: 1, + duration: 5000 + }).start(); + }; + + const fadeOut = () => { + // Will change fadeAnim value to 0 in 5 seconds + Animated.timing(fadeAnim, { + toValue: 0, + duration: 5000 + }).start(); + }; + + return ( + + + Fading View! + + +